开发者

Pass Parameters between views-MVC-3-Not TempData Approach

I have a drop down in one view, I have to use the selected value of this drop down in other view.

I do not want to use Tempdata Approach as it开发者_开发百科 is not best practice.

Is there any better approach for this.

Please give the best practice solution.

Thank you Hari


I can modify this is you provide me with a larger picture (read: more code) of what's already in your views

First view (your List<SelectListItem> will probably differ)

@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" />
}

Then in the controller

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; }
}

And in the second view, Step2.cshtml (assuming Razor)

@model Yournamespace.Step2ViewModel

<div>@Model.MyValue</div>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜