栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

MVC 4使用Bootstrap编辑模式形式

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MVC 4使用Bootstrap编辑模式形式

您应该使用局部视图。我使用以下方法:

使用视图模型,这样就不会将域模型传递给视图:

public class EditPersonViewModel{    public int Id { get; set; }   // this is only used to retrieve record from Db    public string Name { get; set; }    public string Age { get; set; }}

在你的

PersonController:

[HttpGet] // this action result returns the partial containing the modalpublic ActionResult EditPerson(int id){      var viewModel = new EditPersonViewModel();    viewModel.Id = id;    return PartialView("_EditPersonPartial", viewModel);}[HttpPost] // this action takes the viewModel from the modalpublic ActionResult EditPerson(EditPersonViewModel viewModel){    if (ModelState.IsValid)    {        var toUpdate = personRepo.Find(viewModel.Id);        toUpdate.Name = viewModel.Name;        toUpdate.Age = viewModel.Age;        personRepo.InsertOrUpdate(toUpdate);        personRepo.Save();        return View("Index");    }}

接下来,创建一个名为的局部视图

_EditPersonPartial
。其中包含模态页眉,正文和页脚。它还包含Ajax表单。它是强类型的,并包含在我们的视图模型中。

@model Namespace.ViewModels.EditPersonViewModel<div ><button type="button"  data-dismiss="modal" aria-hidden="true">×</button>    <h3 id="myModalLabel">Edit group member</h3></div><div>@using (Ajax.BeginForm("EditPerson", "Person", FormMethod.Post,         new AjaxOptions         {  InsertionMode = InsertionMode.Replace,  HttpMethod = "POST",  UpdateTargetId = "list-of-people"         })){    @Html.ValidationSummary()    @Html.AntiForgeryToken()    <div >        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Name)        @Html.Bootstrap().ControlGroup().TextBoxFor(x => x.Age)    </div>    <div >        <button  type="submit">Save</button>    </div>}

现在在您的应用程序中的某处,说另一个_peoplePartial.cshtml等:

<div>   @foreach(var person in Model.People)    {        <button  data-id="@person.PersonId">Edit</button>    }</div>// this is the modal definition<div  id="edit-person">    <div id="edit-person-container"></div></div>    <script type="text/javascript">    $(document).ready(function () {        $('.edit-person').click(function () { var url = "/Person/EditPerson"; // the url to the controller var id = $(this).attr('data-id'); // the id that's given to each button in the list $.get(url + '/' + id, function (data) {     $('#edit-person-container').html(data);     $('#edit-person').modal('show'); });        });     });   </script>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388349.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号