我正在开发一个简单的mvc应用程序。代码如下:
模型.cs:
public class CustomModel
{
public IEnumerable<lang> lstlang { get; set; }
public IEnumerable<org> lstOrg { get; set; }
}
public class lang
{
public int langid { get; set; }
public string langName { get; set; }
}
public class org
{
public int orgId { get ;set;}
public string orgName { get; set; }
}
Controller.cs
public Action Index()
{
// Get data from database and fill the model
var model = new CustomModel();
return View(model);
}
public Action Partial()
{
// Get data from database and fill the model
var model = new CustomModel();
return PartialView(model);
}
[HttpPost]
public Action Partial(FormCollection frm, CustomModel model)
{
// Get data from database and fill the model
var model = new CustomModel();
return PartialView(model);
}
Index.cshtml
@model CustomModel
@Html.TextboxFor(x => x.lang.FirstOrDefault().id);
<input type="button" id="btn" />
@Html.RenderPartial("Partial", model)
Partial.cshtml
@model CustomModel
@Html.TextboxFor(x => x.lang.FirstOrDefault().id);
<input type="submit" id="submit" />
问题是,当我单击Partial.cshtml
页面中的提交按钮,并在public Action Partial(FormCollection frm, CustomModel model)
中的httppost方法中检查模型时,模型包含null
两个列表{{1} }和lstlang
,但lstOrg
会给出所选的文本框值。
我缺少什么,或者这是使用部分视图的正确方法?
答案 0 :(得分:3)
不要使用FirstOrDefault()
。如果您想使用集合将某些内容发布回前端,则需要使用索引编制。
Public class CustomModel
{
public ICollection<lang> lstlang { get; set; }
public ICollection<org> lstOrg { get; set; }
}
@HTML.textboxfor(x=>x.lang[0].id);