MVC 强类型视图数据与数组
我有一个使用类似于此的强类型 ViewData 的视图:
namespace Site.web2.Models
{
public class MySubData
{
public string Note { get; set; }
public bool IsValid { get; set; }
}
public class MyViewData
{
public int DataId { get; set;}
public List<MySubData> SubData { get; set; }
public MyViewData()
{
}
public void LoadDummyData()
{
DataId = 42;
SubData = new List<MySubData>();
SubData.Add(new MySubData() { Note = "Item 1" });
SubData.Add(new MySubData() { Note = "Item 2" });
}
}
}
控制器:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Test1()
{
Site.web2.Models.MyViewData data = new Site.web2.Models.MyViewData();
data.LoadDummyData();
return View(data);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test1(Site.web2.Models.MyViewData data)
{
return RedirectToAction("Index");
}
视图如下所示:
<%@ Page Title="" Language="C#" MasterPageFile="~/Content/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Site.web2.Models.MyViewData>"
%>
<%= Html.BeginForm<HomeController>(c => c.Test1(null)) %>
<p>Id:</p>
<p><%= Html.TextBox("DataId")%></p>
<p>Note 1:</p>
<p><%= Html.TextBox("SubData[0].Note")%></p>
<p>Note 2:</p>
<p><%= Html.TextBox("SubData[1].Note")%></p>
<input type="submit" value="Submit" />
<% Html.EndForm(); %>
好的,如果我创建一个 MyViewData,调用 LoadDummyData(),并在视图中使用它,我看不到我的数据在文本框中。
有趣的是,如果我在文本框中输入数据,这些数据将填充到 Post 控制器中返回的 MyViewData 中。
我究竟做错了什么? 这个问题在 MVC 的更高版本中修复了吗? 我想我有 RC 1。
Keith
更新 1
这
<%= Html.TextBox("DataId")%>
工作得很好。 我想我的问题是,这应该以
<%= Html.TextBox("SubData[0].Note")%>
同样的方式工作吗?
I have a view that uses a strongly typed ViewData similar to this:
namespace Site.web2.Models
{
public class MySubData
{
public string Note { get; set; }
public bool IsValid { get; set; }
}
public class MyViewData
{
public int DataId { get; set;}
public List<MySubData> SubData { get; set; }
public MyViewData()
{
}
public void LoadDummyData()
{
DataId = 42;
SubData = new List<MySubData>();
SubData.Add(new MySubData() { Note = "Item 1" });
SubData.Add(new MySubData() { Note = "Item 2" });
}
}
}
Controller:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Test1()
{
Site.web2.Models.MyViewData data = new Site.web2.Models.MyViewData();
data.LoadDummyData();
return View(data);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Test1(Site.web2.Models.MyViewData data)
{
return RedirectToAction("Index");
}
And the View is like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Content/Site.Master"
Inherits="System.Web.Mvc.ViewPage<Site.web2.Models.MyViewData>"
%>
<%= Html.BeginForm<HomeController>(c => c.Test1(null)) %>
<p>Id:</p>
<p><%= Html.TextBox("DataId")%></p>
<p>Note 1:</p>
<p><%= Html.TextBox("SubData[0].Note")%></p>
<p>Note 2:</p>
<p><%= Html.TextBox("SubData[1].Note")%></p>
<input type="submit" value="Submit" />
<% Html.EndForm(); %>
Ok, if I create a MyViewData, call LoadDummyData(), and use that in a View, I don't see my data in the TextBoxes.
The funny thing is, if I enter data in to the TextBoxes, that will get populated into the returned MyViewData in the Post controller.
What am I doing wrong? Was this fixed in a later version of MVC? I think I have RC 1.
Keith
Update 1
This
<%= Html.TextBox("DataId")%>
works just fine. I guess my question is, should this
<%= Html.TextBox("SubData[0].Note")%>
work the same way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个:
Try this: