我在部分视图上有一个下拉列表控件,在发布表单后,不会保留下拉列表控件的状态
以下是部分视图cshtml代码:
@model MvcApplication1.Models.TestCriteriaConsolidated
@using (Html.BeginForm("Results", "Home"))
{
<div style="float:left">
<div style="font-weight:bold">
@Html.LabelFor(m => m.testCriteria.Year)
</div>
<div style="width:110px; ">
@Html.DropDownListFor(m => m.testCriteria.Year, new SelectList(new[]
{
new SelectListItem{ Text="All Years", Value=" " },
new SelectListItem{ Text="2010", Value="2010" },
new SelectListItem{ Text="2009", Value="2009" },
new SelectListItem{ Text="2008", Value="2008" },
new SelectListItem{ Text="2007", Value="2007" },
new SelectListItem{ Text="2006", Value="2006" }
}, "Value", "Text", "1"), new { @style = "width: 100px; margin-top: 5px" })
</div>
</div>
<div style="float:left">
<div>
</div>
<div style="width:100px; ">
<input type="submit" value="Results" />
</div>
</div>
}
这是我的控制器代码:
public ActionResult Index()
{
return View();
}
public ActionResult Results()
{
string year = Request.Form["testCriteria.Year"];
List<TestCriteriaResults> test = new List<TestCriteriaResults>();
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "one", Value3="three", Value4="Four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3="three", Value4="four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
TestCriteriaConsolidated combined = new TestCriteriaConsolidated();
combined.testCriteriaResults = test;
return PartialView(combined);
}
新修改的行动方法(这仍然不保持状态):
public ActionResult Results()
{
string year = Request.Form["testCriteria.Year"];
List<TestCriteriaResults> test = new List<TestCriteriaResults>();
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "one", Value3="three", Value4="Four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3="three", Value4="four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
TestCriteriaConsolidated combined = new TestCriteriaConsolidated();
TestCriteria criteria = new TestCriteria() { Year = year };
combined.testCriteriaResults = test;
combined.testCriteria = criteria;
return PartialView(combined);
}
答案 0 :(得分:1)
以下代码为您的下一个局部视图创建视图模型:
TestCriteriaConsolidated combined = new TestCriteriaConsolidated();
combined.testCriteriaResults = test;
您需要在此模型上设置年份:
combined.testCriteria.Year = Request.Form["testCriteria.Year"];
答案 1 :(得分:0)
combined.testCriteria.Year
需要使用您发布的表单值进行设置
试试这个
[HttpPost]
public ActionResult Results(TestCriteriaConsolidated form)
{
List<TestCriteriaResults> test = new List<TestCriteriaResults>();
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "one", Value3 = "three", Value4 = "Four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
TestCriteria criteria = new TestCriteria() { Year = form.testCriteria.Year };
TestCriteriaConsolidated combined = new TestCriteriaConsolidated
{
testCriteriaResults = test,
testCriteria = criteria
};
return View(combined);
}
public ActionResult Results()
{
return View(new TestCriteriaConsolidated());
}