ASPNET MVC 5 HTML帮助程序动作方法视图结果输入ID缺失(有时..)可能的错误? [英] 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(在表单控制器类中)
public class FormController : Controller
{
[HttpGet]
public ActionResult MultiForms()
{
return View("MultiForms");
}
// ...
MultiForms.cshtml-剃刀视图
@Html.Action("MyFormGet", "Form")
关于action方法的通知,我调用了控制器方法,该方法返回了窗体局部视图.
Notice in the action method I call the controller method that returns the form partial view.
MyFormGet操作
[HttpGet]
public PartialViewResult MyFormGet()
{
var viewModel = new MyFormViewModel();
ViewData.TemplateInfo.HtmlFieldPrefix = Guid.NewGuid().ToString().Replace("-", "_");
return PartialView("MyForm", viewModel);
}
查看模型
public class MyFormViewModel
{
public string TextInput1 { get; set; }
}
MyForm.cshtml-剃刀视图
@model Mvc5App.Controllers.MyFormViewModel
@using (Html.BeginForm("MyFormPost", "Form", FormMethod.Post))
{
<br />
@Html.TextBoxFor(m => m.TextInput1)
}
当我将浏览器指向/Form/MultiForms
时,我会得到预期的输出:
When I point my browser at /Form/MultiForms
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">
id属性在哪里?这是ASPNET MVC5中的错误,还是该设置做错了什么?
Where did the id attribute go? Is this a bug in ASPNET MVC5 or have I done something wrong with this setup?
推荐答案
感谢@ 演示我想我明白了当ID不显示时...
Thanks to @demo I think I figured out when the id doesn't show up...
如果ID以数字(或下划线)开头,则不会呈现.因此,此GUID将呈现:
If the id starts with a digit(or underscore) it will not render. So this GUID will render:
f3b1a447_3786_4472_9dfe_14c3ae8aae24
但是这个不会:
1c26ce3d_5c71_408a_aae2_7be414f1d383
上述解决方案的前缀似乎是这样的:
The solution for the above seems to be prefixing with something like this:
FOO_1c26ce3d_5c71_408a_aae2_7be414f1d383
问题就消失了.
这篇关于ASPNET MVC 5 HTML帮助程序动作方法视图结果输入ID缺失(有时..)可能的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!