我在一个视图中有一个下拉列表,我必须在其他视图中使用该下拉列表的选定值。
我不想使用Tempdata方法,因为它不是最佳实践。
有没有更好的方法。
请给出最佳实践解决方案。
谢谢Hari
我可以修改这一点,因为你可以为我提供你的视图中已经存在的内容的更大图片(阅读:更多代码)
第一视图(您的List<SelectListItem>
可能会有所不同)
@using (Html.BeginForm("Step2", "Silly")) {
@Html.DropDownList("NameOfDropDown", new List<SelectListItem>()
{
new SelectListItem()
{
Text = "Label 1",
Value = "1"
},
new SelectListItem()
{
Text = "Label 2",
Value = "2"
}
})
<input type="submit" value="Submit" />
}
然后在控制器中
public class SillyController : Controller
{
[HttpPost]
public ActionResult Step2(string NameOfDropDown)
{
// if the only value being passed is a string, you'll need
// to wrap it in something like a view model class
return View(new Step2ViewModel() { MyValue = NameOfDropDown });
}
}
public class Step2ViewModel()
{
public string MyValue { get; set; }
}
在第二个视图中,Step2.cshtml(假设Razor)
@model Yournamespace.Step2ViewModel
<div>@Model.MyValue</div>