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;
  }
rumi
  • 3,293
  • 12
  • 68
  • 109

2 Answers2

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)
matt_lethargic
  • 2,706
  • 1
  • 18
  • 33
  • 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` – rumi Mar 26 '15 at 17:03
  • ...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. – matt_lethargic Mar 26 '15 at 17:15
  • 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. :-) – matt_lethargic Mar 26 '15 at 17:17
  • 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. :-/ – matt_lethargic Mar 26 '15 at 17:48
  • Sorry the issue is probably not the return statement . Its `ModelState.IsValid` which is false because of that error. – rumi Mar 26 '15 at 17:51
  • I'm aware, I was just saying that this is the only thing that would be odd in your sample code. – matt_lethargic Mar 26 '15 at 17:53
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.