Here ans Scenario:
I have setup couple of ViewModels like:
public class ThreadEditorView
{
public int ForumID { get; set; }
public ThreadEditorPartialView ThreadEditor { get; set; }
public ThreadEditorSpecial ThreadSpecial { get; set; }
}
Now I have and View:
@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post,
new {@enctype="multipart/form-data"})) {
@Html.ValidationSummary(true)
<fieldset>
@Html.Partial("_ThreadEditor", Model.ThreadEditor)
@Html.Partial("_SpecialProperties", Model.ThreadSpecial)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Question is how do I pass data from partial views into controller ?
I know I can simply do this:
public ActionResult NewThread(ThreadEditorView modelEditor,
ThreadEditorPartialView blabla, ThreadEditorSpecial zumg)
But that doesn’t look really convenient, I’d like to pass everything in ThreadEditorView.
Update:
SpecialView
@model Vaniv.Core.ViewModel.Forums.ThreadEditorSpecial
<div class="editor-label">
@Html.LabelFor(model => model.IsSticky)
</div>
<div class="editor-field">
@Html.RadioButtonFor(model => model.IsSticky, false)
@Html.ValidationMessageFor(model => model.IsSticky)
</div>
(some irrevalnt forms)
<div class="editor-field">
@Html.EditorFor(model => model.IsLocked)
@Html.ValidationMessageFor(model => model.IsLocked)
</div>
And Editor:
@model Vaniv.Core.ViewModel.Forums.ThreadEditorPartialView
<legend>ThreadEditorPartialView</legend>
@Html.HiddenFor(model => model.ForumID)
<div class="editor-label">
@Html.LabelFor(model => model.ThreadName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ThreadName)
@Html.ValidationMessageFor(model => model.ThreadName)
</div>
(some forms, that are irrevelant)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Message)
</div>
<div class="editor-field">
@Html.TextAreaFor(model => model.Message)
@Html.ValidationMessageFor(model => model.Message)
</div>
I would recommend you using editor templates instead of partials as they will take care of generating proper names for your input fields so that the model binder is able to correctly resolve the values in the POST action:
and have the corresponding editor templates (note that the names and location of the editor templates are important):
~/Views/Shared/EditorTemplates/ThreadEditorPartialView.cshtml:and
~/Views/Shared/EditorTemplates/ThreadEditorSpecial.cshtml:Now your controller action could simply look like this: