jQuery ASP.NET MVC:如何“模型绑定”一个非 html-helper 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14353245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:29:17  来源:igfitidea点击:

ASP.NET MVC: How to 'model bind' a non html-helper element

jqueryasp.net-mvcentity-frameworkrazormodel-binding

提问by dan

Could you tell me a way(s) that I can bind a modelproperty to a html-element, created without using html helper?

你能告诉我一种方法,我可以将模型属性绑定到 html 元素,而不使用 html helper 创建吗?

In other words to a plain html element such as: <input type="text" />

换句话说,对于一个普通的 html 元素,例如: <input type="text" />

回答by moribvndvs

If you are referring to Model Binding, it does not require helpers, but naming convention. Helpers just make it easy and concise to create the HTML markup.

如果您指的是Model Binding,它不需要帮助程序,而是需要命名约定。助手只是让创建 HTML 标记变得简单和简洁。

You could create plain HTML inputs and just set the nameattribute correctly. The default naming convention is just dot based, omitting the parent level entity's name, but qualifying it from there.

您可以创建纯 HTML 输入并name正确设置属性。默认命名约定只是基于点,省略父级实体的名称,但从那里限定它。

Consider this controller:

考虑这个控制器:

public class MyControllerController : Controller
{
     public ActionResult Submit()
     {
         return View(new MyViewModel());
     }

     [HttpPost]
     public ActionResult Submit(MyViewModel model)
     {
            // model should be not null, with properties properly initialized from form values
            return View(model);
     }
}

And this model:

而这个模型:

public class MyNestedViewModel
{
    public string AnotherProperty { get; set; }
}

public class MyViewModel
{
    public MyViewModel()
    {
         Nested = new MyNestedViewModel();
    }

    public string SomeProperty { get; set; }

    public MyNestedViewModel Nested  { get; set; }
}

You could create the following form purely in HTML:

您可以纯粹在 HTML 中创建以下表单:

<form method="POST" action="MyController/Submit">
    <div><label>Some property</label><input type="text" name="SomeProperty" /></div>
    <div><label>Another property</label><input type="text" name="Nested.AnotherProperty" /></div>
    <button type="submit">Submit</button>
</form>

If you want to display the posted values (in the second Submitoverload), your HTML will have to be modified render the model properties. You'd place this in a view, in this case using Razor syntax and called Submit.cshtml:

如果要显示发布的值(在第二个Submit重载中),则必须修改 HTML 来呈现模型属性。你可以把它放在一个视图中,在这种情况下使用 Razor 语法并调用Submit.cshtml

@model MyViewModel
<form method="POST" action="MyController/Submit">
    <div><label>Some property</label><input type="text" name="SomeProperty" value="@Model.SomeProperty" /></div>
    <div><label>Another property</label><input type="text" name="Nested.AnotherProperty" value="@Model.Nested.SomeProperty" /></div>
    <button type="submit">Submit</button>
</form>

So, this can be done without helpers, but you'd want to use them as much as possible.

因此,这可以在没有助手的情况下完成,但您希望尽可能多地使用它们。

回答by Darin Dimitrov

Just give it a name:

给它一个名字:

<input type="text" name="foo" />

and then inside your controller action simply have an argument with the same name:

然后在您的控制器操作中只有一个同名的参数:

public ActionResult Process(string foo)
{
    // The foo argument will contain the value entered in the 
    // corresponding input field
}