【问题标题】:Redirect Page based on dropdown information in ASp.NET mvc基于 ASp.NET mvc 中的下拉信息重定向页面
【发布时间】:2015-09-24 01:45:35
【问题描述】:

我有一个下拉控件,有两种类型的值“承包商”和“全职”,根据下拉选择将显示相关控件。

Now when i click on the submit button i have to redirect to different action method (i.e) when 'Contractor' is selected it should redirect to AddContractor() else it should redirect to Full time along with the filled information.

如何使用单个 Html.Beginform() 实现它

【问题讨论】:

  • 你不能使用Html.Beginform()(它的html是在发送到视图之前在服务器上生成的,所以它只能包含初始值)。您需要回发所选选项的值。显示您的代码!

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller


【解决方案1】:

好的,你可以这样做:

在视图中:

    @{
    List<SelectListItem> items = new List<SelectListItem>();

    items.Add(new SelectListItem { Text = "Contractor", Value = "0" });

    items.Add(new SelectListItem { Text = "Full time", Value = "1" });
}

@using (Html.BeginForm("Go", "Home", FormMethod.Post))
{
    @Html.DropDownList("Example", items)

    <input type="submit" value="Submit" />
}

<p>Value selected: @ViewBag.Info</p>  

在控制器上:

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Go(int example)
    {
        if (example != null)
        {
            if (example == 0)
            {
                return RedirectToAction("AddContractor", new { info = example});
            }
            else
            {
                return RedirectToAction("Other", new {info = example});
            }
        }
        return View("Index");
    }

    public ActionResult AddContractor(int info)
    {
        ViewBag.Info = info;
        return View("Index");
    }

    public ActionResult Other(int info)
    {
        ViewBag.Info = info;
        return View("Index");
    }

您可以为您的项目使用其他视图和其他信息。我只是编写示例来重定向操作和模型信息。

希望能帮到你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 2013-05-15
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多