0

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>
2
  • I'm thinking that the "irrelevant fields" might not be that irrelevant... Are those fields not part of ThreadEditorPartialView? Have you tried removing them from the partial to see if it works? Commented Jun 8, 2011 at 15:30
  • Those "irrevelant fields" are just genrated by scaffolding, and looks like those I have posted. I can't remove them, because I need all of them for input data. Commented Jun 8, 2011 at 15:35

2 Answers 2

1

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) 
{ 
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Working. Nice. Didn't know about EditorTemplates.
0

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) { }

2 Comments

It's exactly how they are typed. But it doesn't seem to work.
@Łukasz - Could you post the code for at least one of the 2 partials?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.