1

I have a view which calls a child action:

@Html.Action("RenderPostMessage", "JobSurface")

The controller is like this:

public ActionResult RenderPostMessage()
{
    PostMessageViewModel postMessageViewModel = new PostMessageViewModel();
    return PartialView("PostMessage", postMessageViewModel);
}

The partial this calls is like this:

@model PostMessageViewModel

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
}
@using (Html.BeginUmbracoForm<JobSurfaceController>("HandlePostMessage", new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)       

    <p>
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </p>

    <p>
        @Html.LabelFor(model => model.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
    </p>
    <p><button class="button">Post Message</button></p>
}

The 'handle post message' controller is like this:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HandlePostMessage(PostMessageViewModel model)
{
    // Some logic
}

I have a bunch of variables in the view that I need to somehow pass in to the form (as hidden input fields perhaps?) but although I know how to create hidden inputs on the partial, I've no idea how to populate them with the values from the view.

Could anyone suggest how to get the value passed through to the controller?

Many thanks.

3
  • 1
    @Html.Action("RenderPostMessage"... but your ActionResult signature is HandlePostMessage (is this just a typo?). Since you have a view model, why not just just add test as a public property of the view model? Commented May 20, 2014 at 23:32
  • Thanks @Stephen. I'm a noob at MVC so still trying to get my head around this. I think it may be a bit more complicated, so I'll update my original post with more details. Commented May 20, 2014 at 23:41
  • You asked to "populate [the variables] with the values from the view", but you accepted the answer that populates the variables from the controller. Why? Also, since you have a 'PostMessageViewModel', why would you ever venture into dynamic territory by using the ViewBag...simply make them properties on PostMessageViewModel. Commented May 21, 2014 at 1:14

2 Answers 2

1

I have a bunch of variables in the view that I need to somehow pass in to the form (as hidden input fields perhaps?)

It's simple, if you want to render a hidden input field with a value then add it to the ViewBag object in the view.

For instance, if you want to add the content of a variable to the form then in the view you do this:

ViewBag.Foo = "Some Value";

Then in the cshtml file you add the hidden field:

@Html.Hidden("Foo")

This way you will receive the value in the form post.

EDIT: this is how your code should look.

public ActionResult RenderPostMessage()
{
    PostMessageViewModel postMessageViewModel = new PostMessageViewModel();

    // here you set as many values as you want to receive in the form post.
    ViewBag.SomeField = "Some Value";

    return PartialView("PostMessage", postMessageViewModel);
}

View

@model PostMessageViewModel

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);
}
@using (Html.BeginUmbracoForm<JobSurfaceController>("HandlePostMessage", new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)       

    @Html.Hidden("SomeField")

    <p>
        @Html.EditorFor(model => model.Message)
        @Html.ValidationMessageFor(model => model.Message)
    </p>

    <p>
        @Html.LabelFor(model => model.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
    </p>
    <p><button class="button">Post Message</button></p>
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this. I now see that a hidden input is created with name="Foo" attribute, but it doesn't contain a value (when I view in browser dev tools). Should it? Once I know for sure then I can move forward to try to get the value in the code and put it to use.
@Dan did you set the value in the Viewbag. You need to set the value you want in the hidden field.
1

@Html.Action has a parameter 'routeValues' which is an anonymous object. You can pass values there. So...from view to action:

@Html.Action("RenderPostMessage", routeValues:new{SurfaceType = "JobSurface", OtherValue = "Something", NewValue = "Something else"});

Action accepts these route values as method parameters:

    public ActionResult RenderPostMessage(string surfaceType, string otherValue, string newValue)
    {
        var viewModel = new PostMessageViewModel();
viewModel.SurfaceType = surfaceType;
viewModel.OtherValue = otherValue;
viewModel.NewValue = newValue;
        return PartialView("PostMessage", viewModel);
    }

Done!

Comments

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.