ASP.NET MVC3 how to send data from partial view

Here's ans Scenario: I have a couple of ViewModels, for example:

public class ThreadEditorView { public int ForumID { get; set; } public ThreadEditorPartialView ThreadEditor { get; set; } public ThreadEditorSpecial ThreadSpecial { get; set; } } 

Now I have a 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: how to transfer data from partial representations to the controller? I know that I can just do this:

 public ActionResult NewThread(ThreadEditorView modelEditor, ThreadEditorPartialView blabla, ThreadEditorSpecial zumg) 

But it doesn’t look very convenient, I would like to pass everything to 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 the 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 disordered)

  </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> 
0
asp.net-mvc asp.net-mvc-3
source share
2 answers

I would recommend you use editor templates instead of partial ones, since they will take care of creating your own names for your input fields so that the connecting device can correctly resolve 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 appropriate editor templates (note that the names and layout 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 might look something like this:

 public ActionResult NewThread(ThreadEditorView modelEditor) { ... } 
+1
source share

What do _ThreadEditor and _SpecialProperties look like?

If partial numbers are dialed on ThreadEditorPartialView and ThreadEditorSpecial , respectively, ASP.NET MVC will display text fields with names that will help you bind the model accordingly to POST :

_ThreadEditor:

 @model ThreadEditorPartialView // rest of the view here 

_SpecialProperties:

 @model ThreadEditorSpecial // rest of the view here 

With correctly selected elementary elements, your action requires only one parameter:

 public ActionResult NewThread(ThreadEditorView modelEditor) { } 
0
source share

All Articles