3

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.

Fro1
  • 33
  • 5
  • possible duplicate of [ASP.NET MVC 4 override emitted html name and id](http://stackoverflow.com/questions/14810100/asp-net-mvc-4-override-emitted-html-name-and-id) – James Sep 16 '15 at 14:56
  • you dont need the @ when setting the name .. just use `new { Name = "bb-post" })` – JamieD77 Sep 16 '15 at 14:59
  • The accepted answer in the question was actually wrong and i accidentally skipped it after trying it and not working. The answer below it DID work though @James , I edited my question to provide that information. – Fro1 Sep 16 '15 at 15:16

2 Answers2

1

What's wrong with doing

<input id="bb-post" name="bb-post" type="hidden" value="@model.bb_post" />
James
  • 80,725
  • 18
  • 167
  • 237
  • Well I'm doing a huge post with about 20 inputs and I'm doing all of them with the format as is: @Html.HiddenFor(model => model.appName) @Html.HiddenFor(model => model.texans) @Html.HiddenFor(model => model.rockets) @Html.HiddenFor(model => model.astros) @Html.HiddenFor(model => model.returnurl) @Html.HiddenFor(model => model.texans2) @Html.HiddenFor(model => model.astros2) @Html.HiddenFor(model => model.rockets2) Is there something wrong with me wanting to keep it all the same? – Fro1 Sep 16 '15 at 14:45
  • I actually stated in my post that I would like it to all stay the same. If it is not possible then I would just do this. – Fro1 Sep 16 '15 at 14:48
  • @AndreiM yes it does, it's a working solution. It may not be the solution the OP wants but it's a solution all the same. – James Sep 16 '15 at 19:00
0

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

Community
  • 1
  • 1
Thomas Mulder
  • 740
  • 10
  • 26
  • I just tried this and although it changed the Id to "bb-post" and Name="bb-post" there is still a name="bb_post". So Now there are two Name values one with lower case and one with upper case. – Fro1 Sep 16 '15 at 14:58
  • @Fro1 Okay for me it seemed to work, but apparently this is a behavior of chrome cleaning up the html.. This answer by nemesv gives a more detailed explanation: http://stackoverflow.com/a/14810169/2422173.. Hope this helps! – Thomas Mulder Sep 16 '15 at 15:01