Im using EF model in my project. Im using viewmodel in my controllers show data. However, I can't access db.entry in my repository. How come I can't access it?
This my repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MCT.ViewModels;
using System.Data.Entity;
using System.Data;
namespace MCT.Models
{
public class AdministrationRep
{
Model1Container _db = new Model1Container();
public void changequstion(Question _question)
{
}
In my repository I'd like to use
db.entry(model).State = EntityState.Modified;
Or is there another way to use edit for my controller?
In the ObjectContext API you can change the state as follows
db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
Related
I have asp.net MVC5 project
I allowed all actions in Home controller anonymously
but now the requirement is to prevent 2 actions and make them accessible only if user is logged in.
below code is not working means showing jobapply page without login
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Data.Entity;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using myproject.Models;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Net;
using System.Net.Mail;
using System.IO;
using Microsoft.AspNet.Identity;
using System.Text.RegularExpressions;
using System.Configuration;
namespace myproject.Controllers
{
[AllowAnonymous] /************Controller level AllowAnonymous************/
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public HomeController()
{
//code
}
[Authorize]/***********************this is not working*************/
public ActionResult Jobapply(int id)
{
return View();
}
[Authorize]/***********************this is not working*************/
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Jobapply(VacancyApplication vacancyApp)
{
return View();
}
}
}
You can use the [Authorize] Attribute at the controller level and individually allow the actions which you want to allow without authorization apply [AllowAnonymous] attribute on that particular actions.
I am currently building an MVC site using Entity Framework. I have created following class:
using System; using System.Collections.Generic;
using System.Linq; using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
namespace .Models {
public class VehicleTableData
{
[NotMapped]
public Dictionary<string,string> StandardVehicleData { get; set; }
[NotMapped]
public Dictionary<string,string> AdditionalData { get; set; }
} }
However, I would like it to be ignored by Entity Framework as when I try to create a view with it I get the error that there is no valid key.
If you have the class defined in your DbContext with the following line of code:
public DbSet<VehicleTableData> VehicleTableDatas { get; set; }
This will cause Entity Framework to include the class. Once the above line is removed it will not be included.
You could also remove the [NotMapped] attributes as this would only apply to properties that you would not want saved to the database in a model included in your DbContext.
I have a bit of a problem. I have created all my models in a different Class Library. Now, I have to create a controller in a MVC Web Application using the models present in the Class Library. I am not able to see the Models that I created in the Class Library while creating a controller in MVC. How do I proceed on getting all the models in Class Library to the MVC project. I have even added a reference of the Class Library to the MVC project.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainClasses;
namespace TestingModel.Controllers
{
public class ProductsGroupController : Controller
{ //
// GET: /ProductsGroup/
public ActionResult Index()
{
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web;
namespace DomainClasses.Models
{
public class ProductGroupMaster
{
public int ProductGroupID { get; set; }
}
}
Why is this? I have a feeling that it has something to do with my controller.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NidecMotorXref.MvcUI.BaseControllers;
using System.Web.Mvc;
using LINQtoCSV;
using System.IO;
[HttpPost]
public virtual ActionResult Matches(ImportModel model, string save, HttpPostedFileBase fileUpload)
{ ... }
These are all my references but File.Delete("myfileName") does not resolve while my higher up acted like it should. Even though my solution works with System.IO.File I was curious why it doesn't resolve my reference?
Controller defines a method called File, therefore the compiler will choose this method when resolving the symbol File.
When used within a class that does not include a File symbol then the compiler will fall back on resolving via the using statements, hence File.Delete working in other classes.
In ASP.NET MVC is there a way to enumerate the controllers through code and get their name?
example:
AccountController
HomeController
PersonController
would give me a list such as:
Account, Home, Person
Using Jon's suggestion of reflecting through the assembly, here is a snippet you may find useful:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
public class MvcHelper
{
private static List<Type> GetSubClasses<T>()
{
return Assembly.GetCallingAssembly().GetTypes().Where(
type => type.IsSubclassOf(typeof(T))).ToList();
}
public List<string> GetControllerNames()
{
List<string> controllerNames = new List<string>();
GetSubClasses<Controller>().ForEach(
type => controllerNames.Add(type.Name));
return controllerNames;
}
}
You can reflect through your assembly and find all classes which inherit from type System.Web.MVC.Controller. Here's some sample code that shows how you could do that:
http://mvcsitemap.codeplex.com/WorkItem/View.aspx?WorkItemId=1567
All who using this post better read this post before: using Assembly.GetCallingAssembly() not returns the calling assembly
The issue is that razor views are acting as independent dynamic assemblies and you don't get the desired assembly.
Yair
Create the property in every controller and then you get the name like this.