C# 编辑-在索引视图中保存而不转到编辑视图
我有一个MVC-C应用程序。它有几个页面包含由SCA折叠向导生成的C# 编辑-在索引视图中保存而不转到编辑视图,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我有一个MVC-C应用程序。它有几个页面包含由SCA折叠向导生成的创建,编辑,删除功能。一切正常。我需要执行另一个无法自动生成的操作 就是 我有一个带有实体框架的数据模型和一个视图模型 索引所需的所有数据都可用并以html表的形式显示 我将表的1列更改为@EditorFor,以便可以对值进行更改 我想要的是一种将视图内容以及我在可编辑控件中更改的数据传递给控制器的方法。我不想要的是转到另一个编辑视图并在那里进行编辑 我尝试了以下方法: 使用(Html.BeginForm())放置@并将保存按钮
创建
,编辑
,删除
功能。一切正常。我需要执行另一个无法自动生成的操作
就是
@EditorFor
,以便可以对值进行更改- 使用(Html.BeginForm())放置
@并将保存按钮设置为提交按钮
- 使用动作链接按钮传递模型值,如下所示,并在控制器中的
编辑方法中获得更改
<a href="@Url.Action("Edit","InvoicePayments", new { PaymentId = item.PaymentId, InvoiceId = item.InvoiceId,
这是一把.net小提琴public class HomeController : Controller { [HttpGet] public ActionResult Index() { return View(new HelloWorldMvcApp.MasterViewModel()); } [HttpPost] public ActionResult Index(HelloWorldMvcApp.MasterViewModel model) { //your value is stored here and sent back to controller in model container //you can see the value you edited in return page FieldTwoColum return View(model); } }
using System; using System.ComponentModel.DataAnnotations; namespace HelloWorldMvcApp { public class SampleViewModel { public SampleViewModel() { this.FieldOne = "1"; this.FieldTwo = "2"; this.FieldThree = "3"; } public string FieldOne { get; set; } public string FieldTwo { get; set; } public string FieldThree { get;set; } } } using System; using System.ComponentModel.DataAnnotations; namespace HelloWorldMvcApp { public class SampleViewModel { public SampleViewModel() { this.FieldOne = "1"; this.FieldTwo = "2"; this.FieldThree = "3"; } public string FieldOne { get; set; } public string FieldTwo { get; set; } public string FieldThree { get;set; } } } @model HelloWorldMvcApp.SampleViewModel @{ Layout = null; } <!DOCTYPE html> <!-- template from http://getbootstrap.com/getting-started --> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <!-- CSS Includes --> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="col-md-6 col-md-offset-3"> @using (Html.BeginForm()) { <div class="form-group"> @Html.LabelFor(m => m.FieldOne) @Model.FieldOne @Html.LabelFor(m => m.FieldTwo) @Model.FieldTwo @Html.LabelFor(m => m.FieldThree) @Model.FieldThree @Html.TextBoxFor(model => model.FieldThree, new {@class="form-control"}) </div> <button type="button" class="btn btn-success submit">Edit</button> } <br/><br/> <div class="alert alert-warning fade"> <img src="http://entechprod.blob.core.windows.net/dotnetfiddle/morpheus.jpg" style="max-width:100%;"/><br/><br/> <strong><span class="alert-content"></span></strong> </div> </div> </div> <!-- JS includes --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script> </body> </html>
网游内容 基本上,您可以使用强类型模型作为索引,并将值作为该模型的一个实例传递给控制器,该模型具有编辑的值使用系统; 使用System.ComponentModel.DataAnnotations; 命名空间HelloWorldMvcApp { 公共类SampleViewModel { 公共SampleViewModel() { 此字段为“1”; 此字段为“2”; 此字段3=“3”; } 公共字符串FieldOne{get;set;} 公共字符串字段二{get;set;} 公共字符串字段三{get;set;} } } 使用制度; 使用System.ComponentModel.DataAnnotations; 命名空间HelloWorldMvcApp { 公共类SampleViewModel { 公共SampleViewModel() { 此字段为“1”; 此字段为“2”; 此字段3=“3”; } 公共字符串FieldOne{get;set;} 公共字符串字段二{get;set;} 公共字符串字段三{get;set;} } } @模型HelloWorldMvcApp.SampleViewModel @{ 布局=空; } 引导101模板 @使用(Html.BeginForm()) { @LabelFor(m=>m.FieldOne) @模型。现场一号 @LabelFor(m=>m.FieldTwo) @模型二 @LabelFor(m=>m.FieldThree) @型号3 @TextBoxFor(model=>model.FieldThree,新的{@class=“formcontrol”}) 编辑 }
看法public class SampleViewModel { public SampleViewModel() { } public SampleViewModel(string f1, string f2, string f3 ) { this.FieldOne = f1; this.FieldTwo = f2; this.FieldThree = f3; } public string FieldOne { get; set; } public string FieldTwo { get; set; } public string FieldThree { get;set; } } public class MasterViewModel { public MasterViewModel() { this.svmLst = new List<SampleViewModel>(); SampleViewModel svm1 = new SampleViewModel("a", "b", "c"); SampleViewModel svm2 = new SampleViewModel("p", "", "r"); SampleViewModel svm3 = new SampleViewModel("z", "y", "z"); svmLst.Add(svm1); svmLst.Add(svm2); svmLst.Add(svm3); } public List<SampleViewModel> svmLst { get; set; } }
我不知道,为什么我的问题没有得到回答!!:(至少给我一个提示或任何建议。请标记答案,因为编码之神正在监视你:D,或者在评论中再次询问,如果你认为这是错误的:(顺便说一句,这并不是错的,菲德尔说,祝你长寿,繁荣昌盛:P@Deepani你得到答案了吗?如果有的话,你可以单击勾选按钮将其标记为答案谢谢你的答案。但不幸的是,这并不能解决我提出的问题。在我看来,我在HTML表中有一个对象列表。我需要其中一列可编辑(与代码中的@model HelloWorldMvcApp.MasterViewModel @{ 布局=空; } 引导101模板 @DisplayNameFor(model=>model.svmLst[0].FieldOne) @DisplayNameFor(model=>model.svmLst[0].FieldTwo) @DisplayNameFor(model=>model.svmLst[0].FieldThree) @使用(Html.BeginForm()) { 对于(int i=0;i Model.svmLst[i].FieldOne) @Html.HiddenFor(modeleItem=>Model.svmLst[i].FieldOne) @EditorFor(modeleItem=>Model.svmLst[i].FieldTwo) @DisplayFor(modelItem=>Model.svmLst[i].FieldThree) @Html.HiddenFor(modeleItem=>Model.svmLst[i].FieldThree) } }
类似)。问题是,当我单击“编辑”按钮时,我确实会将数据传递到控制器中的操作方法。如果可以这样做,我可以保存该记录。它有点长。因此,我将告诉您所提供的代码有什么不同。视图中的模型应为@Html.TextBoxFor
,数据与@model IEnumerable
编辑视图中的数据不同ode>,相反,它在
索引视图中循环并填充。其中一个字段是EditorFor,我需要用户在那里键入的值传递给控制器。我对您给我的相同代码进行了更改,以便您可以找到我期望的内容。请运行应用程序,在[POST]中设置断点索引并查看编辑的值是否出现。它不是。这是我的问题。@Deepani检查它是否是您的答案请标记答案否则注释我为您创建了一个ViewModel,在该ViewModel中,您可以迭代较小视图模型的实例基本上较大视图模型有较小视图的列表
@model HelloWorldMvcApp.MasterViewModel @{ Layout = null; } <!DOCTYPE html> <!-- template from http://getbootstrap.com/getting-started --> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 101 Template</title> <!-- CSS Includes --> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> </head> <body> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.svmLst[0].FieldOne) </th> <th> @Html.DisplayNameFor(model => model.svmLst[0].FieldTwo) </th> <th> @Html.DisplayNameFor(model => model.svmLst[0].FieldThree) </th> <th></th> </tr> @using (Html.BeginForm()) { for(int i=0;i<Model.svmLst.Count();i++) { <tr> <td> @Html.DisplayFor(modelItem => Model.svmLst[i].FieldOne) @Html.HiddenFor(modelItem => Model.svmLst[i].FieldOne) </td> <td> @Html.EditorFor(modelItem => Model.svmLst[i].FieldTwo) </td> <td> @Html.DisplayFor(modelItem => Model.svmLst[i].FieldThree) @Html.HiddenFor(modelItem => Model.svmLst[i].FieldThree) </td> <td> <input type="submit" class="btn btn-success" value="Edit"/> </td> </tr> } } </table> <!-- JS includes --> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> <script src="//ajax.aspnetcdn.com/ajax/mvc/4.0/jquery.validate.unobtrusive.min.js"></script> </body> </html>