在视图之间传递参数-MVC-3-Not TempData Approach

时间:2011-06-09 12:53:57

标签: asp.net-mvc-3

我在一个视图中有一个下拉列表,我必须在其他视图中使用此下拉列表的选定值。

我不想使用Tempdata方法,因为这不是最佳做法。

有没有更好的方法呢?

请提供最佳实践解决方案。

谢谢 哈

1 个答案:

答案 0 :(得分:0)

我可以修改这是你为我提供了更大的图片(阅读:更多代码)你的观点中已有的内容

第一个视图(您的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>
相关问题