1、设置模型,引入构造函数,初始化集合

public class Person
{
public Person() //引入构造函数,初始化集合.如果未设置构造函数,集合会出现错误。
{
Skills = new HashSet<string>();
BirthDate = DateTime.Now.AddDays(-20);
}

public int PersonId { get; set; }

[Required]
public string FirstName { get; set; }

[Required]
public string LastName { get; set; }

[Required]
public DateTime BirthDate { get; set; }

[Required]
[UIHint("BooleanButtonLabel")] //给指定字段指定显示模板。用于html模板辅助方法,比如html.DisplayForModel
public bool LikesMusic { get; set; }

[Required]
[EmailAddress]
public string EmailAddress { get; set; }

public ICollection<string> Skills { get; set; }
}

2、设置控制器方法

public ActionResult Create()
{
var person = new Person();
return View(person);
}

[HttpPost]
public ActionResult Create(Person person)
{
if (ModelState.IsValid)
{
_people.Add(person);
return RedirectToAction("Index");
}

return View(person);

}

3、使用基架生成强类型视图

@model BootstrapMVC30Days.Models.Person

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">

<div class="form-group">
@Html.LabelFor(model => model.LikesMusic, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.LikesMusic)
@Html.ValidationMessageFor(model => model.LikesMusic, "", new { @class = "text-danger" })
</div>
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Skills, htmlAttributes: new { @class ="control-label col-md-2"})
<div class="col-md-10">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" id="add-skill" type="button">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
<input type="text" id="skill" class="form-control" placeholder="输入然后点击 + 加入" />

</div>
</div>
</div>

<div id="skills-wrapper"></div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<ul id="skills-list" class="list-group"></ul>
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>

4、在页面底部引入脚本   。虽然我们加入了多个同名(name相同)的隐藏表单,但模型绑定会将它作为集合来处理。

@section Scripts {
<script>
$(function () {
$("#add-skill").click(function () {
//取得文本框的值
var skill = $("#skill").val();

//加入隐藏输入到表单
$("#skills-wrapper").append($("<input type='hidden' name ='Skills' value='" + skill + "' />"));

//加入输入到列表框用于显示
$("#skills-list").append($("<li class='list-group-item'> " + skill + "</li>"));

//重置表单,文本框获得焦点
$("#skill").val("").focus;
});

});
</script>
@Scripts.Render("~/bundles/jqueryval")
}

使用Jquery动态加入对象的集合属性,提交集合属性到表单的更多相关文章

  1. jQuery 获取对象 根据属性、内容匹配, 还有表单元素匹配

    指定元素中包含 id 属性的, 如: $("span[id]") 代码如下: <span id="span1" name="S1"&g ...

  2. jQuery 基础 : 获取对象 根据属性、内容匹配, 还有表单元素匹配

    指定元素中包含 id 属性的, 如: $("span[id]")   <span id="span1" name="S1">AA ...

  3. 为何jquery动态添加的input value无法提交到数据库?【坑】

    有两个输入框,我想让第一个输入框失去焦点以后,第二个输入框自动获取第一个输入框的value为默认值,jquery代码如下,可以正常显示,但是用PHP提交数据,并插入数据库的时候确实空值,尚未查找到原因 ...

  4. jQuery插件:Ajax将Json数据自动绑定到Form表单

    jQuery注册方法的两种常用方式: //jQuery静态方法注册 //调用方法$.a1() $.extend({ a1: function () { console.log("a1&quo ...

  5. 【jquery采坑】Ajax配合form的submit提交(微擎表单提交,ajax验证,submit提交)

    1.采坑:实现form的submit提交,在提交之前,进行ajax的不同校验,然后onsubmit=return check(),进行提交 1/1 目的:可以实现以 from的submit提交,然后还 ...

  6. 原 form 表单中 disabled 属性的元素不参与表单提交

    https://blog.csdn.net/benben683280/article/details/79173336

  7. jQuery实现form表单序列化转换为json对象功能示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. 【.net+jquery】绘制自定义表单(含源码)

    前言 两年前在力控的时候就想做一个类似的功能,当时思路大家都讨论好了,诸多原因最终还是夭折了.没想到两年多后再这有重新提出要写一个绘制表单的功能.对此也是有点小激动呢?总共用时8.5天的时间基本功能也 ...

  9. HTML5 表单新增属性

    1. 表单内元素的form属性 在H5中可以把form放到页面的任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了 <form id=&quo ...

随机推荐

  1. bzoj 2463 [中山市选2009]谁能赢呢? 博弈

    [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3014  Solved: 2165[Submit][Status][D ...

  2. ng-repeat的作用域问题

    ng-repeat会创建一个子作用域,所以在ng-repeat下面要使用参数时,要用$parent.XXXX参数. 示例如下:

  3. Codevs 2666 2666 Accept Ratio

    时间限制: 1 s  空间限制: 32000 KB   题目等级 : 钻石 Diamond 题目描述 Description 某陈痴迷于pku的ACM题库,常常彻夜奋斗刷题.他最近的目标是在NOIP0 ...

  4. django学习之- Form

    参考:http://www.cnblogs.com/wupeiqi/articles/6144178.htmlFORM中的字段只对post上来的数据进行form验证,主要涉及:字段 和 插件字段:对用 ...

  5. Go --- 设计模式(工厂模式)

    简易工厂主要是用来解决对象“创建”的问题.以下的例子取自<大话设计模式>中第一章,实现一个可扩展的“计算器”.当增加新的功能时,并不需改动原来已经实现的算法.由于是简易工厂,所以我们还是需 ...

  6. [Angular] Refactor Angular Component State Logic into Directives

    Allow the base toggle to be a tag (<toggle>) or attribute (<div toggle>). The <toggle ...

  7. ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library

    ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library 须要安装pcre包. sudo apt-get upd ...

  8. Windows驱动程序开发基础(四)驱动的编译调试和安装

    Windows驱动程序开发基础,转载标明出处:http://blog.csdn.net/ikerpeng/article/details/38793995 以下说一下开发出来驱动程序以后怎样编译.一般 ...

  9. 搭建Maven私服(使用Nexus)

    搭建私服能够做什么? 1.假设公司开发组的开发环境所有内网.这时怎样连接到在互联网上的Maven中央仓库呢? 2.假设公司常常开发一些公共的组件.怎样共享给各个开发组.使用拷贝方式吗?假设这样,公共库 ...

  10. UVA 10288 - Coupons(概率递推)

    UVA 10288 - Coupons option=com_onlinejudge&Itemid=8&page=show_problem&category=482&p ...