简体   繁体   English

asp.net mvc HTML Helper隐藏字段的行为奇怪

[英]asp.net mvc Html helper hidden field behaving strangely

The behavior of the HtmlHelper.Hidden eludes me. HtmlHelper.Hidden的行为使我难以理解。

This is from a template inside Views/Shared/DisplayTemplates/Test.cshtml. 这来自Views / Shared / DisplayTemplates / Test.cshtml中的模板。 Idea was to output a delete button next to the element in the list. 想法是在列表中的元素旁边输出一个删除按钮。 So the view accepts a model of IEnumerable and then uses @Html.EditorForModel() to output each test item. 因此,该视图接受IEnumerable模型,然后使用@ Html.EditorForModel()输出每个测试项目。

So if i put this into Test.cshtml: 因此,如果我将其放入Test.cshtml:

<input type="hidden" name="Name" value="@Model.Name"/>
@Html.Hidden("Name2", Model.Name) 

That yields this: 这样得出:

<input type="hidden" name="Name" value="test"/> 
<input id="RoleList_12__Name2" name="RoleList[12].Name2" type="hidden" value="test" />

Why does the name of the Html helper render something different from what i tell it to? 为什么HTML Helper的名称与我告诉的名称有所不同? I assume that this is intended, but i don't understand why. 我认为这是故意的,但我不明白为什么。

Update 更新资料

How would i go ahead and retrieve RoleList[12].Name in my controller? 我将如何在控制器中检索RoleList [12] .Name?

My delete function accepts: 我的删除功能接受:

[HttpPost]        
public ActionResult DeleteRole(Roles.Test model)
{
}

How can that one accept a RoleList[12] name item? 那该如何接受RoleList [12]名称项目? It always returns null if i try. 如果我尝试它总是返回null。

It's so that the (default) model binding will work; 这样(默认)模型绑定将起作用; ie when posted the field will be resolved to RoleList[12].Name in the Controller that takes a parameter equivalent to the model class. 例如,发布后,该字段将解析为Controller中的RoleList [12] .Name,该名称采用与模型类等效的参数。

If your controller action doesn't reference the model class the data will silently be discarded, so that you don't have to post back entire data - but that which is posted back will be realized for you. 如果您的控制器操作未引用模型类,则数据将被静默丢弃,这样您就不必回发整个数据-但是将为您实现回发的数据。

You can override the default model binding; 您可以覆盖默认的模型绑定。 see The Features and Foibles of ASP.NET MVC Model Binding 请参见ASP.NET MVC模型绑定的功能和缺点


The problem with your delete is that it's at the wrong level; 删除的问题在于级别错误; either you bind something to the instance of the enumerable - wrapped up in its own form (ok for small amounts of data) or you decide to have an @ActionLink into which the ID of the enumerable is passed. 您可以将某些东西绑定到可枚举的实例(以其自身的形式包装(可以存储少量数据)),也可以决定使用@ActionLink来将可枚举的ID传递给该实例。 I generally do the second, something like: 我通常会执行第二步,例如:

@Html.ActionLink("Del", "DeleteRole", "Controller", new { Id = item.Id}, new { @class = "btn"})

Of course the above doesn't need a [Post] action so that'd need changing on your controller. 当然,上述操作不需要执行[Post]操作,因此需要在控制器上进行更改。

The above would sit in your display or edit template as appropriate. 以上内容将显示在您的显示器中,或根据需要修改模板。

This is happening because your view accepts IEnumerable of some model type. 发生这种情况是因为您的视图接受某种模型类型的IEnumerable。 You basically have a list of objects passed to the view and since they all have a property named Name the binder is trying to distinguish between different instances by giving them names like model[index]property. 基本上,您有一个传递给视图的对象列表,并且由于它们都具有名为Name的属性,因此活页夹试图通过给它们提供诸如model [index] property之类的名称来区分不同的实例。

If you're trying to delete an instance you should have an Html.ActionLink link that invokes some action of your controller which takes the unique ID of the instance you're trying to delete. 如果您要删除实例,则应该有一个Html.ActionLink链接,该链接调用控制器的某些操作,该操作采用您要删除的实例的唯一ID。 Something like this in your view: 您认为这样的事情:

@foreach (var item in model)
{
   @Html.ActionLink("linkText", "DeleteRole", "controllerName", new {id = item.RoleId})
}

and then your controller should have an action like this: 然后您的控制器应执行以下操作:

public ActionResult DeleteRole(int id)
{
    // Logic to delete role based on provided role ID.
    return View();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM