How To Send Along Non-Model Data From Html.BeginForm?
Umbracians,
I am building a site in Umbraco 7, using MVC4. I have a working form from which I can successfully post data back to the controller.
However, I would like to include two radio buttons on the form that are not bound to the model.
The radio buttons will let the user pick whether their last name is equal to their birth name. If true, then I want to send this along to the controller, so that I can assign the LastName property to the BirthName property.
In my controller, I have the following code:
public RedirectResult Submit(User user, Dictionary<string, object> htmlAttributes)
{
// some code...
return Redirect(this.Request.UrlReferrer.ToString());
}
In my razor view, I have the following code:
@using (Html.BeginForm("Submit", "MemberAddressForm", FormMethod.Post, new { Name = "Jay" }))
{
// strongly typed, working form declaration
// two non-model radio buttons
<label for="rbLastNameEqualsBirthNameTrue">Yes</label>
<input type="radio" id="rbLastNameEqualsBirthNameTrue" name="lastNameEqualsBirthName" value="true" checked="checked" />
<label for="rbLastNameEqualsBirthNameFalse">No</label>
<input type="radio" id="rbLastNameEqualsBirthNameFalse" name="lastNameEqualsBirthName" value="false" />
}
My first problem is that I am not getting key/value pair {Name="Jay"} in my controller htmlAttributes parameter. It does hold two parameters, but those are for the controller name and the controller action. Why is my own custom key/value pair not being added? Am I not accessing my html attributes in the right way?
My second problem is that I need to figure out how to send along the non-model radiobuttons' value, rather than my own hard coded name. How can I do this?
Is what I'm trying to do even possible? If so, is this also the best practice? If not, then how should I do it? Should I modify the model?
Okay, so I figured out I can pass a parameter using Html.RenderAction, just so long as the passed parameter's name matches the one I've got setup in my routing.
However, it's still not working for Html.BeginForm.
Also... I have a new requirement:
The member address form is used on multiple pages. Based on which page the form is used on, it needs to redirect to a specific confirmation page.
So PageA.aspx needs to redirect to PageA-Thanks.aspx and PageB.aspx needs to redirect to PageB-Thanks.aspx.
What's the best practice for accomplishing this without having to hardcode any such redirects? I would prefer it if the user would be able to set a RedirectPage property on PageA, so that my code could make use of it to perform the proper redirect.
How do I do this? Should I pass along @Umbraco.Field("RedirectPage") to my Html.RenderAction, which passes it on via the ViewBag to the member address form, which then posts it back to the controller, which then performs the proper redirect?
How To Send Along Non-Model Data From Html.BeginForm?
Umbracians,
I am building a site in Umbraco 7, using MVC4. I have a working form from which I can successfully post data back to the controller.
However, I would like to include two radio buttons on the form that are not bound to the model.
The radio buttons will let the user pick whether their last name is equal to their birth name. If true, then I want to send this along to the controller, so that I can assign the LastName property to the BirthName property.
In my controller, I have the following code:
In my razor view, I have the following code:
My first problem is that I am not getting key/value pair {Name="Jay"} in my controller htmlAttributes parameter. It does hold two parameters, but those are for the controller name and the controller action. Why is my own custom key/value pair not being added? Am I not accessing my html attributes in the right way?
My second problem is that I need to figure out how to send along the non-model radiobuttons' value, rather than my own hard coded name. How can I do this?
Is what I'm trying to do even possible? If so, is this also the best practice? If not, then how should I do it? Should I modify the model?
Okay, so I figured out I can pass a parameter using Html.RenderAction, just so long as the passed parameter's name matches the one I've got setup in my routing.
However, it's still not working for Html.BeginForm.
Also... I have a new requirement:
The member address form is used on multiple pages. Based on which page the form is used on, it needs to redirect to a specific confirmation page.
So PageA.aspx needs to redirect to PageA-Thanks.aspx and PageB.aspx needs to redirect to PageB-Thanks.aspx.
What's the best practice for accomplishing this without having to hardcode any such redirects? I would prefer it if the user would be able to set a RedirectPage property on PageA, so that my code could make use of it to perform the proper redirect.
How do I do this? Should I pass along @Umbraco.Field("RedirectPage") to my Html.RenderAction, which passes it on via the ViewBag to the member address form, which then posts it back to the controller, which then performs the proper redirect?
I'm just gonna bump this to see if I can get some replies now that we've had new years...
Never mind, I already figured it out.
For those of you struggling with the same problem, here's how to pass parameters from view to controller via submit.
Controller:
View:
The above BeginForm call puts parameters in the generated form's action element. These parameters are matched by name to the called controller action.
I've also figured out how to make a certain page redirect to a certain other page.
In my controller index I pass along the current page's ID to the partial view:
In my partial view, I pass the page ID back to the controller like so:
The controller's submit action can now retrieve the desired redirection page ids from the current page's properties, like so:
You're welcome! :D
Hello,
Did you figure out how to use Html.BeginForm to send along the non-model radiobuttons' value, rather than my own hard coded name?
Thank you
is working on a reply...