Reputation: 1239
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>
Upvotes: 0
Views: 4365
Reputation: 1038850
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:
@using (Html.BeginForm("NewThread", "Thread", FormMethod.Post,
new {@enctype="multipart/form-data"})) {
@Html.ValidationSummary(true)
<fieldset>
@Html.EditorFor(x => x.ThreadEditor)
@Html.EditorFor(x => x.ThreadSpecial)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
and have the corresponding editor templates (note that the names and location of the editor templates are important):
~/Views/Shared/EditorTemplates/ThreadEditorPartialView.cshtml
:
@model 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>
and ~/Views/Shared/EditorTemplates/ThreadEditorSpecial.cshtml
:
@model ThreadEditorSpecial
...
Now your controller action could simply look like this:
public ActionResult NewThread(ThreadEditorView modelEditor)
{
...
}
Upvotes: 1
Reputation: 16184
What do _ThreadEditor
and _SpecialProperties
look like?
If the partials are typed to ThreadEditorPartialView
and ThreadEditorSpecial
respectively ASP.NET MVC will render the textboxes with names that help it bind the model appropriately on POST
:
_ThreadEditor:
@model ThreadEditorPartialView
// rest of the view here
_SpecialProperties:
@model ThreadEditorSpecial
// rest of the view here
With properly typed partials your action only needs to take one parameter:
public ActionResult NewThread(ThreadEditorView modelEditor) { }
Upvotes: 0