[英]Mapping naming convention for parameters in actions for asp.net core
I have a razor view with that does 我有一个剃刀视图
<form asp-action="Action">
@Html.TextBoxFor(m => m.User.Name)
</form>
when submitting the form I would like to get the name of the user as a parameter, but with user
, userName
, user_Name
the property doesnt get mapped. 在提交表单时,我希望将用户的名称作为参数,但是对于user
, userName
, user_Name
,属性不会被映射。
What is asp.net core expecting as a name so that it gets mapped correctly? 什么是asp.net核心期望作为名称,以便正确映射?
public ActionResult Action(MyModel m) // not desired
public ActionResult Action(string name) // tried userName and user_Name
{
}
The name
of the input being generated in this scenario is User.Name
. 在此方案中生成的输入的name
是User.Name
。 You can't call your parameter User.Name
, of course, as this isn't a valid C# identifier. 当然,您无法调用参数User.Name
,因为这不是有效的C#标识符。 A simple way to make this work is to instruct the Model-Binder to use User.Name
for your name
parameter using the FromForm
attribute. 使这项工作的一种简单方法是指示Model-Binder使用FromForm
属性将User.Name
用于您的name
参数。 eg: 例如:
public ActionResult Action([FromForm(Name = "User.Name")] string name)
{
}
With this approach, name
can be anything you like. 通过这种方法, name
可以是您喜欢的任何name
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.