Model:
faculty class
[Key]
[Required(ErrorMessage="*Enter Faculty id")]
[StringLength(5)]
public string Id { get; set; }
[Required(ErrorMessage="*Enter First Name")]
[MaxLength(30)]
public string F_Name { get; set; }
[Required(ErrorMessage="*Enter Last Name")]
[MaxLength(30)]
public string L_Name { get; set; }
[MaxLength(30)]
public string M_Name { get; set; }
[Required(ErrorMessage="*Enter Email id")]
[MaxLength(50)]
public string Email{ get; set; }
[Required(ErrorMessage="*Enter Department")]
public Int16 Dept_Id { get; set; }
[ForeignKey("Dept_Id")]
public virtual Department Dept { get; set; }
Department class:
public class Department //static table
{
[Key]
public Int16 Id { get; set; }
[Required]
[MaxLength(20)]
public string Dept_Name { get; set; }
}
View:
<div class="editor-label">
Faculty Id
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Id, new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.Id,null, new { style="color:Red"})
</div>
<div class="editor-label">
First Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.F_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.F_Name, null, new { style="color:red;"})
</div>
<div class="editor-label">
Middle Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.M_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.M_Name)
</div>
<div class="editor-label">
Last Name
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.L_Name,new { style = "width:200px;"})
#Html.ValidationMessageFor(model => model.L_Name, null, new { style="color:red;"})
</div>
<div class="editor-label">
Department
</div>
<div class="editor-field">
#Html.DropDownListFor(model =>model.Dept_Id ,ViewBag.Dept_Id as IEnumerable<SelectListItem>,
string.Empty,new { style = "width:200px;text-align:center;"})
#Html.ValidationMessageFor(model => model.Dept_Id, null, new { style="color:red;"})
</div>
<p>
<input type="submit" value="Create" />
</p>
Controller:
[HttpPost]
public ActionResult Create(Faculty faculty)
{
faculty.Email = faculty.F_Name + "." + faculty.L_Name + "#mitcoe.edu.in";
if (ModelState.IsValid)
{
db.Faculty.Add(faculty);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Dept_Id = new SelectList(db.Department, "Id", "Dept_Name", faculty.Dept_Id);
return View(faculty);
}
The drop down list works fine and the ID for all the fields is fetched properly from the view.
But in the post function the modelstate is invalid as the Dept_name is null.
ORM creates the tables with only Dept_Id as a foreign key.
Why is the controller expecting Dept_Name also? Is anything wrong with my models?
The controller is expecting Dept_Name because:
You are model binding to the Faculty class.
Department is a component of Faculty.
Dept_Name is a required field on Department.
Dept_Name is null when the model binding parses the form data,
as you do not have it as an input anywhere in you view.
Two suggestions, either:
Have a separate FacultyInputModel class which is
the parameter to the Create action method. FacultyInputModel includes only those properties which you expect to be returned from the form;
Or: Use HiddenFor with the Dept_Name property, so it is included in your form data posted from you view and the model state is valid.
I'd recommend the first personally. It can be useful sometimes to separate your view models, i.e. what data you are displaying, from your input models, i.e. what data you expect to be posted back. It does add more classes and complexity though, on the down side. See e.g. https://www.simple-talk.com/dotnet/asp.net/the-three-models-of-asp.net-mvc-apps/ for a bit about input models.
Related
I send data from view to action. Chrome browser shows that Genre_Id is sent properly as it you see the attached screenshot but when it is received by the action, Visual Studio raises exception and shows it as Null / 0 as you see the attached screenshot. So why does this happen?
this is Movie Model
public class Movie
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
[Display(Name="Created on")]
public DateTime DateAdded { get; set; }
[Required]
[Display(Name = "Release Date")]
public DateTime ReleaseDate { get; set; }
[Required]
[Display(Name = "Number in Stock")]
public int NumberInStock { get; set; }
public Genre Genre { get; set; }
[Display(Name="Genre")]
public int Genre_Id;
}
this is the Form
#using(Html.BeginForm("Save", "Movies"))
{
<div class="form-group">
#Html.LabelFor(m => m.Movie.Name)
#Html.TextBoxFor(m => m.Movie.Name, new { #class="form-control"})
</div>
<div class="form-group">
#Html.LabelFor(m => m.Movie.DateAdded)
#Html.TextBoxFor(m => m.Movie.DateAdded, new { #class="form-control"})
</div>
<div class="form-group">
#Html.LabelFor(m => m.Movie.ReleaseDate)
#Html.TextBoxFor(m => m.Movie.ReleaseDate, new { #class="form-control"})
</div>
<div class="form-group">
#Html.LabelFor(m => m.Movie.NumberInStock)
#Html.TextBoxFor(m => m.Movie.NumberInStock, new { #class="form-control" })
</div>
<div class="form-group">
#Html.LabelFor(m => m.Movie.Genre_Id)
#Html.TextBoxFor(m => m.Movie.Genre_Id, new { #class="form-control"})
</div>
#Html.HiddenFor(m => m.Movie.Id)
<button type="submit" class="btn btn-primary">Save</button>
}
Look at the icons, the Genre_Id member looks different. That's because it's a field, not a property.
MVC requires model members to be properties. Add { get; set; } to its declaration.
I simplified my problem as i could ,I have two classes Employee ,Departments
I supposed to make a one to many relationship between them
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string salary { get; set; }
public virtual Department depatments { get; set; }
}
//////////////////////
public class Department
{
public Department()
{
this.employees = new HashSet<Employee>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Employee> employees { get; set; }
}
and that is my context class
public class myEntity :DbContext
{
public DbSet<Employee> employees {get;set;}
public DbSet<Department> Departments {get;set;}
}
well, I have created a Create ActionResult in order to be able to add new record to Employee and filled a selectlist with Department table/Class
And sent it to view by viewbag
[HttpGet]
public ActionResult Create()
{
ViewBag.xx = new SelectList(mycontext.Departments, "Id", "Name");
return View();
}
[HttpPost]
public ActionResult Create(Employee emp)
{
mycontext.employees.Add(emp);
mycontext.SaveChanges();
return RedirectToAction("Index", "Employee");
}
And there is the view
**#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.salary)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.salary)
#Html.ValidationMessageFor(model => model.salary)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.depatments)
</div>
<div class="editor-field">
#Html.DropDownListFor(model =>model.depatments.Id,#ViewBag.xx as SelectList)
#Html.ValidationMessageFor(model => model.salary)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
I ran it, every thing is ok and database created.
the view dropdownlist render ID And Name as it should.
Normally ,Every Employee record should have Department ID as a Foreign Key.
The problem is When i add new record to Employee it add a new record in Department where it should pick just ID and add it to Employee
Note: I filled the Department first With no problem
If you use a different context to save then it should work without this issue. Your Departments entity is still being tracked in the first context and this is the reason for the issue.
Im new to asp.net MVC4 , I have an MVC4 application that is using SQL Server 2012 EntityFramework (code first). The Edit post method is not saving the data. On checking if ModelState.IsValid it return false, can anyone help me find whats wrong with my code
MODEL
public class Customer
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="*")]
public string FirstName { get; set; }
[Required(ErrorMessage = "*")]
public string LastName { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(1, ErrorMessage="Initial only")]
public string MI { get; set; }
[Required(ErrorMessage = "*")]
public string Address { get; set; }
[Required(ErrorMessage = "*")]
public String ContactNo { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.EmailAddress, ErrorMessage = "Invalid Email")]
public string EmailAddress { get; set; }
[Required(ErrorMessage = "*")]
[MaxLength(8, ErrorMessage = "Max of 8")]
[MinLength(5, ErrorMessage = "Min of 5")]
[DataType(DataType.Password)]
[Display(Name = "Password")]
[NotMapped]
public string Password { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
[NotMapped]
[Display(Name = "Retype-PW")]
public string RetypePassword { get; set; }
[Required]
[Display(Name = "How much is")]
[NotMapped]
public string Captcha { get; set; }
}
EDIT VIEW
<h2>Edit CUSTOMER</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Customer</legend>
#Html.HiddenFor(model => model.Id)
#Html.HiddenFor(model=>model.Captcha)
#Html.HiddenFor(model=>model.Password)
#Html.HiddenFor(model=>model.RetypePassword)
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MI)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MI)
#Html.ValidationMessageFor(model => model.MI)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ContactNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ContactNo)
#Html.ValidationMessageFor(model => model.ContactNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmailAddress)
#Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
EDIT CONTROLLER METHOD
public ActionResult Edit(int id = 0)
{
Customer customer = db.Customers.Find(id);
if (customer == null)
{
return HttpNotFound();
}
return View(customer);
}
//
// POST: /Customer/Edit/5
[HttpPost]
public ActionResult Edit(Customer customer)
{
if (ModelState.IsValid)
{
db.Entry(customer).State = EntityState.Modified;
db.SaveChanges();
if (User.IsInRole("Administrator"))
{
return RedirectToAction("Index");
}
else
{
return RedirectToAction("Details");
}
}
return View(customer);
}
Thanks for the help
#Html.HiddenFor(model=>model.Captcha)
#Html.HiddenFor(model=>model.Password)
#Html.HiddenFor(model=>model.RetypePassword)
this lines are not safety, it's possible view the content in console at the same time maybe isn't the good value in password you make a 8 char limit but you read the value in bd, i hope this value is cripted!, you write a code to more to 8 char well the model isn't valid, try to read the value for this champs in the console and compare with your settings.
the problem you use the same model to create and edit, well for edit isn't required the captha and pasword, the options are make other model, setting not required, check if create or edit mode to set required,
help me find whats wrong with my code
You're using the scaffolded #Html.ValidationSummary(true), where true is passed for hidePropertyErrors. This is fine if your edit form shows an editor for each property of your model, but if not, your page will not show what ModelState errors prevented successful processing. Change true to false to see all errors.
You're using Entity Framework models as ViewModels.
You're allowing mass assignment.
You're not using an cross-site request forgery token.
You've just run into the prime case that demonstrates why you should use ViewModels. One solution is to remove model errors for properties you don't want to see as faulty.
Like:
ModelState.Remove("Captcha");
ModelState.Remove("Password");
ModelState.Remove("RetypePassword");
if (ModelState.IsValid)
{
// your code ...
But this will cause your model to be updated with an empty password null for the properties for which you didn't provide an editor, which you don't want to and which will fail anyway. So you'll need to load the existing entity and update it:
ModelState.Remove("Captcha");
ModelState.Remove("Password");
ModelState.Remove("RetypePassword");
if (ModelState.IsValid)
{
var existingCustomer = db.Customers.First(c => c.ID == customer.ID);
// Update properties of attached entity
existingCustomer.FirstName = customer.FirstName;
existingCustomer.LastName = customer.LastName;
// and so on...
To circumvent most of this clumsiness, simply define a ViewModel:
public class EditCustomerModel
{
[Required(ErrorMessage="*")]
public string FirstName { get; set; }
[Required(ErrorMessage="*")]
public string LastName { get; set; }
// and so on...
}
Then in your action method will look like this:
public ActionResult Edit(EditCustomerModel customer)
{
if (ModelState.IsValid)
{
var existingCustomer = db.Customers.First(c => c.ID == customer.ID);
// Update properties of attached entity
existingCustomer.FirstName = customer.FirstName;
existingCustomer.LastName = customer.LastName;
db.SaveChanges();
return RedirectToAction(...);
}
return View();
}
I used CodeCaster 2nd suggestion but it didn't work, but when I put hidden input for my 3 notmapped fields(Captcha, password, retypepassword) on my view and assigning default values it works now.
<input type="hidden" id="Captcha" name="Captcha" value="Captcha" />
<input type="hidden" id="Password" name="Password" value="Password" />
<input type="hidden" id="RetypePassword" name="RetypePassword" value="RetypePassword" />
I am using MVC in order to build a blog. What I want is to save post comments to its corresponding place in the database but it does not work.
My post model is as follows:
public class Post
{
[Key]
public int PostId { get; set; }
public string Title { get; set; }
public DateTime CreatedDate{get;set;}
public DateTime UpdateDate { get; set; }
public string Body { get; set; }
public ICollection<Comment> Comments { get; set;}
public ICollection<Tag> Tags { get; set; }
}
My Comment model is as follows:
public class Comment
{
[Key]
public int CommentId { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post{get; set;}
public string CommentCreateDate { get; set; }
public string CommentUpdateDate { get; set; }
public string CommeterName { get; set; }
public string EmailAddress { get; set; }
public string CommentText { get; set; }
public bool Approved { get; set; }
}
I have the following Action Methods:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
[HttpPost]
[ValidateInput(false)]
public ActionResult CreateComment(int id, string name, string email, string txt, bool aproved = false)
{
Post post = GetPost(id);
Comment comment = new Comment();
comment.Post = post;
comment.CommentCreateDate = DateTime.Now.ToString();
comment.CommeterName = name;
comment.EmailAddress = email;
comment.CommentText = txt;
comment.Approved = aproved;
db.Comments.Add(comment);
db.SaveChanges();
m_commentList.Add(comment);
return RedirectToAction("CreateComment", new { id = id });
}
And in my view I am trying this:
#model Blog.Models.Comment
#{
ViewBag.Title = "CreateComment";
}
<h2>Create a Comment</h2>
#using (Html.BeginForm())
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
I get no exception but none of the data from the form is being saved. Only the data that is set in the action result, that is, CommentCreateDate and Approved. I am not sure what I am doing wrong.
I have tried a second option which is to include the id of the comment in BeginForm() as follows:
#using (Html.BeginForm(new {id = Model.CommentId}))
{
<fieldset>
<legend>Enter Comment</legend>
<div class="editor-label">
#Html.LabelFor(model => model.CommeterName)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.CommeterName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CommentText)
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.CommentText)
</div>
<p>
<input type="submit" value="Create comment" />
</p>
</fieldset>
}
This will give me a null reference exception even if I use the new keyword :
System.NullReferenceException: Object reference not set to an instance of an object.
Why is this happening? Can anybody help?
Thank you
Your action signature should be:
public ActionResult CreateComment(Comment model)
The names generated for the form fields will bind back to the properties of the same model class. There's no way for the framework to know, for example, that the CommenterName property should match up to the name parameter of the action.
Your second example makes very little sense - you're trying to write out the ID but you have never set one. In fact, you don't even pass a Comment to the view with the form, which is why you get a NullReferenceException:
[HttpGet]
public ActionResult CreateComment()
{
return View();
}
Also, you should be careful with what fields you expose to your models and actions. For example, a user could easily force their comment to be approved just by adding the following markup via their browser's development console:
<input type="hidden" name="approved" value="true" />
Anything that is either in your model properties or a parameter to your action can be set by the user.
An altogether better option would be to use a dedicated model class for the form:
public class CreateCommentViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Text { get; set; }
}
Then to map this to your Comment in your action with:
[HttpPost]
public ActionResult CreateComment(CommentViewModel model)
{
var comment = new Comment();
comment.CommenterName = model.Name;
// etc...
}
This prevents the user from being able to set things like Approved and CreatedDate.
I have edit page and controller that creates new model object and fills some data from db into this object then send a model object to view. When I click the submit button, some fields in this object have been cleared.
For example:
Before:
user_id
name
birth_date
username
password
id_role
email
After (Fields that are not null or empty):
name
username
birth_date
The model:
public partial class Users
{
public Users()
{
this.Albums = new HashSet<Albums>();
this.History = new HashSet<History>();
this.Country = new HashSet<Country>();
this.Artists = new HashSet<Artists>();
this.SelectedCountries = new List<string>();
}
[DisplayName("User ID")]
public System.Guid user_id { get; set; }
[DisplayName("Name")]
public string name { get; set; }
[DisplayName("Birth date")]
public Nullable<System.DateTime> birth_date { get; set; }
[DisplayName("Username")]
public string username { get; set; }
[DisplayName("Password")]
public string password { get; set; }
[DisplayName("Rights")]
public System.Guid id_role { get; set; }
[DisplayName("User Email")]
public string email { get; set; }
public bool isRemember { get; set; }
public virtual ICollection<Albums> Albums { get; set; }
public virtual ICollection<History> History { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<Country> Country { get; set; }
public virtual ICollection<Artists> Artists { get; set; }
public virtual List<string> SelectedCountries { get; set; }
}
Edit method:
public ActionResult Edit()
{
if (HttpContext.User.Identity.IsAuthenticated)
{
var userName = HttpContext.User.Identity.Name;
var user = db.Users.Where(x => x.username == userName).FirstOrDefault();
ViewBag.Countries = new MultiSelectList(db.Country, "id_country", "name", user.SelectedCountries);
return View(user);
}
return HttpNotFound();
}
Edit method for handling post request:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Users users)
{
if (ModelState.IsValid)
{
foreach (var country in users.SelectedCountries)
{
var dbCountry = db.Country.Find(new Guid(country));
if (dbCountry != null)
users.Country.Add(dbCountry);
}
db.Entry(users).State = System.Data.Entity.EntityState.Modified;
//There handle of string array goes
db.SaveChanges();
return RedirectToAction("Index");
}
return View(users);
}
View:
<h2>Edit</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Users</legend>
<div class="editor-label">
#Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.name)
#Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.birth_date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.birth_date)
#Html.ValidationMessageFor(model => model.birth_date)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.username)
#Html.ValidationMessageFor(model => model.username)
</div>
<div class="editor-label">
#Html.Label("Country")
</div>
<div class="editor-field">
#Html.DropDownList("SelectedCountries", (ViewBag.Countries as MultiSelectList), new { multiple = "multiple", #class = "chosen", style = "width: 350px;"})
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
Thanks in advance :)
You will only receive values that are in your form. Http is stateless..
What you need to do.. is create a ViewModel. That ViewModel is the subset of properties from your domain model that are displayed in the view. Like this:
public class UserViewModel {
public string Name { get; set; }
public string Username { get; set; }
public DateTime? DateofBirth { get; set; }
}
Use this model in your view. Then, in your controller.. get the user and update the appropriate fields:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserViewModel viewModel) {
var user = db.Users.Where(x => x.username == viewModel.Username).FirstOrDefault();
user.Name = viewModel.Name;
user.Username = viewModel.Username;
// .. etc.
db.SaveChanges();
}
If you are worried about all of the manual mapping involved in this, there exists frameworks to help you with that:
Automapper
ValueInjector
You are heading down a very very daunting path if you start adding hidden fields into your view. Its a maintenance nightmare and very error prone.
The post operation only collects the values you have in the form.
If you want the other values to proceed in your controllers post-method, you can for example, add hidden fields.
#Html.HiddenFor(x => x.HiddenPostBack)