ASP.NET MVC:如何'模型绑定'一个非html辅助元素 [英] ASP.NET MVC: How to 'model bind' a non html-helper element

查看:138
本文介绍了ASP.NET MVC:如何'模型绑定'一个非html辅助元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以告诉我一种可以将模型属性绑定到一个没有使用html帮助器的html元素的方法吗?



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

解决方案

如果您是指模型绑定,它不需要帮助者,而是命名约定。帮助者只需简单而简洁即可创建HTML标记。



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



考虑这个控制器:

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

[HttpPost]
public ActionResult提交(MyViewModel模型)
{
//模型不应为null,属性从表单正确初始化值
return View(model);
}
}

此模型:

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

public class MyViewModel
{
public MyViewModel()
{
嵌套= new MyNestedViewModel();
}

public string SomeProperty {get;组; }

public MyNestedViewModel嵌套{get;组;
}

您可以纯粹以HTML形式创建以下表单:

 < form method =POSTaction =MyController / Submit> 
< div>< label>某些属性< / label>< input type =textname =SomeProperty/>< / div>
< div>< label>另一个属性< / label>< input type =textname =Nested.AnotherProperty/>< / div>
< button type =submit> Submit< / button>
< / form>

如果要显示已发布的值(在第二个 Submit overload),您的HTML将不得不修改渲染模型属性。您可以将其放在一个视图中,在这种情况下使用Razor语法,并调用 Submit.cshtml

  @model MyViewModel 
< form method =POSTaction =MyController / Submit>
< div>< label>某些属性< / label>< input type =textname =SomePropertyvalue =@ Model.SomeProperty/>< / div>
< div>< label>另一个属性< / label>< input type =textname =Nested.AnotherPropertyvalue =@ Model.Nested.SomeProperty/>< / div> ;
< button type =submit> Submit< / button>
< / form>

所以,这可以在没有帮手的情况下完成,但是您最好尽可能地使用它们。


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

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

解决方案

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.

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

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:

<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 Submit overload), 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:

@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.

这篇关于ASP.NET MVC:如何'模型绑定'一个非html辅助元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆