I have the following ViewModel:
public class ProfileViewModel
{
public BandProfileModel BandProfile { get; set; }
public MusicanProfileModel MusicianProfile { get; set; }
public RegularProfileModel RegularProfile { get; set; }
}
I'm using this ViewModel In my View to Post a form:
@using (Ajax.BeginForm("RegisterBand", "NewProfile", new AjaxOptions() { HttpMethod = "Post",
InsertionMode = InsertionMode.Replace
}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-horizontal">
<div class="form-group">
<div class="col-md-10">
Bandname
</div>
<div class="col-md-10">
@Html.EditorFor(x => x.BandProfile.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(x => x.BandProfile.Name, "", new { @class = "text-danger" })
</div>
</div>
Here Is my RegisterBand model:
public ActionResult RegisterBand(BandProfileModel model)
{
if (ModelState.IsValid == false)
{
return Json(JsonRequestBehavior.AllowGet);
}
return View("Index");
}
The problem I have is that the name-attribute of Input fields are BandProfile.Name, BandProfile.Genre, instead of just Genre and Name.
How can I solve this problem?
You could just change your RegisterBand
ActionResult to accept a ProfileViewModel
.
public ActionResult RegisterBand(ProfileViewModel model)
{
if (ModelState.IsValid == false)
{
return Json(JsonRequestBehavior.AllowGet);
}
return View("Index");
}
Your HttpPost
needs to accept an instance of the same object that you pass into the form, otherwise it won't know what names to bind to.