Reputation: 33
Before I ask my question let me state that I DID look online, and I DID look on stack overflow for this question and DID find certain solutions but none of what I found worked for me. So I am asking here to hopefully get an answer to this question and appreciate any help given.
I am doing a Form post to an external website. In my model I created all the values by doing the following in my LoginViewModel:
public string returnurl { get; set; }
In my constructor i set the values needed like so:
returnurl = "www.retururl.com";
In my index page I to the post like so using Razor in MVC 4.0:
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "myForm", @action = Model.loginUrl }))
{
@Html.HiddenFor(model => model.returnurl)
}
When I check on fiddler it does the following behind the scenes:
<input id="returnurl" name="returnurl" type="hidden" value="www.returnurl.com">
It makes the input id AND name the name of the variable returnurl.
My problem is that one of the name values in my form post MUST be "bb-post". I obviously can not name a variable bb-post in my loginViewModel so what I tried was just setting the name to something like bb_post then in my index page.
So i did the following:
@Html.HiddenFor(model => model.bb_post, new { name = "bb-post" })
but this did not work, when i checked fiddler it did the following:
<input id="bb_post" name="bb_post" type="hidden" value="" />
I also tried this:
@Html.HiddenFor(model => model.bb_post, new { @name = "bb-post" })
but got this:
<input id="bb_post" name="bb_post" type="hidden" value="" />
And finally tried this:
@Html.HiddenFor(model => model.bb_post, new { @Name = "bb-post" })
but got this:
<input Name="bb-post" id="bb_post" name="bb_post" Name="bb-post" type="hidden" value="" />
Using the capital name as so @Name gives me two name values. This is not what I am looking for.
I DO realize that I can just write it this way directly in to the html:
<input id="bb_post" name="bb-post" type="hidden" value="" />
But in order to keep it all the looking the same I was hoping there was a way to keep this format:
@Html.HiddenFor(model => model.bb_post, new { @Name = "bb-post" })
Thank you.
Edit: I did find the answer in the Question that was recommended but the accepted answer was not correct. I must have not looked further than the accepted answer by mistake (I usually do and apologize). But the following is what worked if someone else accidentally skips the answer to this for going by the accepted answer which is WRONG because it generates TWO name values. This was the correct answerd and what worked for me:
@Html.Hidden("bb-post", Model.bb_post, new { id="bb-post" })
Thanks.
Upvotes: 3
Views: 739
Reputation: 779
I just tried the following:
@Html.HiddenFor(x => x.Client, new {id="bb-post", @Name = "bb-post"})
and got this result
<input name="bb-post" id="bb-post" type="hidden" value="BDOObject.Client">
So if you set @Name with a capital 'N' AND id, it works.. If I don't set id, it takes the original value bb_post for the id... But @Name with capital N just seemed to work...
EDIT: So apparently it only appeared to work, because Chrome cleaned up the html. But according to this answer by nemesv, you are better off using @Html.Hidden over @Html.Hiddenfor, like so:
@Html.Hidden("bb-post", Model.bb_post, new { id="bb-post"})
Because Html.Hiddenfor tries to divine the name property from your model, which is what makes it different from Html.Hidden
Upvotes: 0
Reputation: 82096
What's wrong with doing
<input id="bb-post" name="bb-post" type="hidden" value="@model.bb_post" />
Upvotes: 1