asp.net-mvc – ASP.NET MVC,将Model从View传递给Controller
发布时间:2020-12-16 09:47:24 所属栏目:asp.Net 来源:网络整理
导读:我遇到了ASP.NET MVC的问题,并将数据从View传递到Controller.我有这样的模型: public class InputModel { public ListProcess axProc { get; set; } public string ToJson() { return new JavaScriptSerializer().Serialize(this); } } public class Proces
我遇到了ASP.NET MVC的问题,并将数据从View传递到Controller.我有这样的模型:
public class InputModel { public List<Process> axProc { get; set; } public string ToJson() { return new JavaScriptSerializer().Serialize(this); } } public class Process { public string name { get; set; } public string value { get; set; } } 我在Controller中创建了这个InputModel并将其传递给View: public ActionResult Input() { if (Session["InputModel"] == null) Session["InputModel"] = loadInputModel(); return View(Session["InputModel"]); } 在我的Input.cshtml文件中,我有一些代码来生成输入表单: @model PROJ.Models.InputModel @using(Html.BeginForm()) { foreach(PROJ.Models.Process p in Model.axProc){ <input type="text" /> @* @Html.TextBoxFor(?? => p.value) *@ } <input type="submit" value="SEND" /> } 现在,当我单击提交按钮时,我想处理放入文本字??段的数据. 问题1:我看过这个@ Html.TextBoxFor(),但我真的没有得到这个“stuff => otherstuff”.我得出结论,“otherstuff”应该是我想要写入数据的字段,在这种情况下它可能是“p.value”.但是箭头前面的“东西”是什么? 回到控制器我然后有一个函数用于POST一些调试: [HttpPost] public ActionResult Input(InputModel m) { DEBUG(m.ToJson()); DEBUG("COUNT: " + m.axProc.Count); return View(m); } 这里Debug只显示如下内容: {"axProc":[]} COUNT: 0 所以我得到的返回模型是空的. 问题2:我是否在使用@using(Html.BeginForm())做了一些根本性的错误?这不是正确的选择吗?如果是这样,我如何让我的模型充满数据回到控制器? 我希望有人可以填写我正在忽略的一些细节. 解决方法
将您的视图更改为此类内容以正确绑定表单提交上的列表.
@using(Html.BeginForm()) { for(int i=0;i<Model.axProc.Count;i++){ <span> @Html.TextBoxFor(model => model.axProc[i].value) </span> } <input type="submit" value="SEND" /> } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 使用Cookie进行Web会话状态 – 有哪些缺陷?
- asp.net – 在自托管Web应用程序中使用性能计数器
- asp.net-mvc – SquishIt MVC – 调试模式,不刷新
- ASP.NET UpdatePanel Javascript回调
- asp.net – PHP的require_once()是否有ASP MVC等效?
- asp.net-mvc – 用于支持多种语言的ASP MVC技术
- 如何在asp.net转发器控件中获取项目点击事件?
- 比较自托管:WCF与HttpListener
- 基于.NET平台常用的框架整理
- asp.net-mvc-2 – ASP.NET MVC和存储库模式理解
推荐文章
站长推荐
- asp.net-mvc-5 – 为什么我不能以种子用户身份登
- ASP.NET导出到Excel – 锁定单元格
- asp.net – dbo.TempGetStateItemExclusive3重复
- asp.net-mvc – 在ASP MVC中,如何将新视图和文件
- asp.net – 如何从Web应用程序进行高级打印?
- asp.net-web-api2 – WEB API 2,Selfhost,Access
- asp.net – Azure AD B2C – 多个子域
- asp.net-mvc – 如何将IoC成员资格提供程序与ASP
- asp.net-mvc – 在ASP.NET身份中角色与声明的最佳
- asp.net-mvc – ASP.NET MVC:通过所有ActionLin
热点阅读