0

I m using a viewmodel on my create view. Every thing works but on form post via jquery I m getting The Id field is required ModelState error. I have seen some solutions regarding adding [Bind(Exclude = "Id")] annotation in the model class but then when I call the same action method on the Update model it never binds the Id of the model and inserts a new record in db.

My viewmodel looks like

public class MemberSiteContactModel { public int Id { get; set; } [Display(Name = "Name")] [Required(ErrorMessage = "Name is required")] public string Name { get; set; } // More items } 

My partial view looks like

@model MemberSiteContactModel <tr class="highlight"> <td class="col-sm-1">@Html.TextBoxFor(x => x.Name, new { @class = "name" })</td> <td class="col-sm-2">@Html.TextBoxFor(x => x.ContactNo, new { @class = "contactNo" })</td> <td class="col-sm-1"><input type="button" class="btn btn-xs btn-success contactSaveRow" value="Save" /></td> <td class="col-sm-1"><input type="button" class="btn btn-xs btn-danger contactDeleteRow" value="Remove" name="btnRemoveContact" /></td> </tr> 

My controller looks like

[HttpPost] [ValidateAntiForgeryToken] public ActionResult SaveSiteContact(MemberSiteContactModel memberSiteContactModel) { if (ModelState.IsValid) { //processing } } 

I have also created a sample application to replicate the behavior

public class CityModel { public int Id { get; set; } [Display(Name = "City")] [Required(ErrorMessage = "City is required")] public string City { get; set; } } public ActionResult Contact() { return View(); } @model WebApplication3.Models.CityModel @using (@Html.BeginForm("SaveCity", "Home", FormMethod.Post)) { @Html.HiddenFor(x => x.Id) @Html.TextBoxFor(x => x.City) <input type="submit" value="Submit" /> } [HttpPost] public ActionResult SaveCity(CityModel cityModel) { if (ModelState.IsValid) { } return null; } 
5
  • 1
    stackoverflow.com/questions/16266988/… Commented Mar 26, 2015 at 15:25
  • You can pass as default on inserting Commented Mar 26, 2015 at 15:27
  • @EhsanSajjad If I set a default value of zero to my Id and check in my action method if its zero then insert or update otherwise seems like a hack but should not there be a better solution Commented Mar 26, 2015 at 15:31
  • @EhsanSajjad tried a hidden field and the value of id comes as 0 but model state still shows the same error i.e. The Id field is required Commented Mar 26, 2015 at 15:43
  • How about making it nullable Commented Mar 26, 2015 at 15:45

2 Answers 2

1

You're not passing the Id in, you need to add a hidden field that holds to Id otherwise how does the Id ever get passed into the posted model.

@Html.HiddenFor(x => x.Id) 
Sign up to request clarification or add additional context in comments.

6 Comments

I did add it. After adding it in the controller action my id gets a value of '0' but modelstate is still false and I get the same message. i.e. The Id field is required
...you didn't need to add anything to the controller?! Perhaps you need to post your actual code so we can see what you're doing.
Also I see no HTML.BeginForm if the view you've posted is a partial then what does the main view look like. Post. More. Code. :-)
There only thing that is not right would be the return null in your save action, the rest is fine and should work with no issues. If your sample application doesn't work with that code I cannot help as its perfect code. :-/
Sorry the issue is probably not the return statement . Its ModelState.IsValid which is false because of that error.
|
0

I'm using MVC and have the same problem, so i check out all my create code and find that i wasn't send an empty model to the View at call time. The solution was simple; on the Create() action result, just add a line that creates a new ViewModel and set it as parameter to the View or Partial View return. Something like this:

public ActionResult Create(){ ViewModel entity = new ViewModel(); return PartialView(entity); } 

I hope that helps.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.