Reputation: 638
I am trying to create a edit page for a viewmodel. The view only shows a control for the string in the view model. How do I get the form to display controls for the other objects? If I manually add controls for the Resource model in the viewmodel the form post the resource as null.
The viewmodel
public class ViewModelResourceReturn
{
public string Teststring { get; set; }
public Resource Resource { get; set; }
public ViewModelResult Result { get; set; }
public List<SelectListItem> RoleCheckboxes { get; set; }
}
The controller
[HttpGet]
public ActionResult AddEditRecord(int? id)
{
ResourceRestApi api = new ResourceRestApi(this);
if (id != null)
{
ViewBag.IsUpdate = true;
ViewModelResourceReturn resource = api.GetResource(id ?? 0);
return PartialView(resource);
}
ViewBag.IsUpdate = false;
return PartialView();
}
The view that is generated
@model Project.ViewModels.ResourceViewModels.ViewModelResourceReturn
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>ViewModelResourceReturn</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Teststring)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Teststring)
@Html.ValidationMessageFor(model => model.Teststring)
</div>
@Html.EditorFor(m => m.Resource)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Editor Template:
@model Project.Models.Resource
@{
Layout = null;
}
@Html.HiddenFor(model => model.ResourceId)
<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>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FullName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FullName)
@Html.ValidationMessageFor(model => model.FullName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TimeManagerResourceId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TimeManagerResourceId)
@Html.ValidationMessageFor(model => model.TimeManagerResourceId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TravelManagerResourceId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TravelManagerResourceId)
@Html.ValidationMessageFor(model => model.TravelManagerResourceId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.OvertimeManagerResourceId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.OvertimeManagerResourceId)
@Html.ValidationMessageFor(model => model.OvertimeManagerResourceId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AbsenceManagerResourceId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AbsenceManagerResourceId)
@Html.ValidationMessageFor(model => model.AbsenceManagerResourceId)
</div>
Edit:
[HttpPost]
public ActionResult AddEditRecord(ViewModelResourceReturn resource)
{
return PartialView(resource);
}
Upvotes: 0
Views: 3714
Reputation: 9469
You have to create the editors for the other properties. It cannot guess how to render the complex ones.
You can use @Html.EditorFor(m => m.Resource)
and create an editor template for the Resource
type
EDIT: In the POST action, try renaming the parameter to something other than resource
. It may cause the model binder to panic a little.
Upvotes: 1