首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC普通html表单绑定

MVC普通html表单绑定
EN

Stack Overflow用户
提问于 2017-05-21 13:59:32
回答 1查看 1.3K关注 0票数 1

我看到了成千上万的例子使用剃须刀BeginForm,我知道它自动绑定控制器输入到提交的信息.无论如何,我想知道在正常情况下是否存在相同的自动绑定。

<form method="get" action="@Url.Action("Index","Home")"> <input type="text" name="foo"> </form>如果它不..。我怎么能把它绑起来..。请不要告诉我使用html助手..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-21 15:10:41

是的,您可以使用普通html进行bind model,但是名称attribute的值必须与modelproperty的名称相同。

代码语言:javascript
复制
public class viewmodel
{
  public string FirstName {get; set;}
  public string  LastName {get; set;}  
}

所以你的html

代码语言:javascript
复制
@model viewmodel

<form method="get" action="@Url.Action("Index","Home")">
      <input type="text" name="FirstName" value="@model.FirstName"> //name should be of same name as property name
      <input type="text" name="LastName" value="@model.LastName">
      <input type='submit' value='Submit'/>
</form>

最后,您的action将是

代码语言:javascript
复制
public ActionResult Index(viewmodel model)
{
   return View(model);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44097759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档