ooXei1sh ooXei1sh - 1 year ago 86
C# Question

ASPNET MVC 5 Html helper Action method view results input id missing (sometimes..) Possible Bug?

To recreate the issue I'm having I've set up two simple controller actions, two views and a view model:

MultiForms Action (in the form controller class)

public class FormController : Controller
public ActionResult MultiForms()
return View("MultiForms");

// ...

MultiForms.cshtml - Razor View

@Html.Action("MyFormGet", "Form")

Notice in the action method I call the controller method that returns the form partial view.

MyFormGet Action

public PartialViewResult MyFormGet()
var viewModel = new MyFormViewModel();
ViewData.TemplateInfo.HtmlFieldPrefix = Guid.NewGuid().ToString().Replace("-", "_");
return PartialView("MyForm", viewModel);

View Model

public class MyFormViewModel
public string TextInput1 { get; set; }

MyForm.cshtml - Razor View

@model Mvc5App.Controllers.MyFormViewModel

@using (Html.BeginForm("MyFormPost", "Form", FormMethod.Post))
<br />
@Html.TextBoxFor(m => m.TextInput1)

When I point my browser at
I get the output I expect:

<input type="text" value="" name="e166fa0d_46fe_40d4_a970_73c52a35f224.TextInput1" id="e166fa0d_46fe_40d4_a970_73c52a35f224_TextInput1">

Then I refresh the page and the output becomes this:

<input type="text" value="" name="48edc339_69ad_4b9b_9583_198038beab45.TextInput1">

Where did the id attribute go? Is this a bug in ASPNET MVC5 or have I done something wrong with this setup?

Answer Source

Thanks to @demo I think I figured out when the id doesn't show up...

If the id starts with a digit(or underscore) it will not render. So this GUID will render:


But this one will not:


The solution for the above seems to be prefixing with something like this:


And the problems go away.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download