0

我有一個下拉控件有兩種類型的值'承包商'和'全職',根據下拉選擇相關的控件將被顯示。重定向頁面基於ASp.NET中的下拉列表信息mvc

現在,當我點擊提交按鈕時,我必須重定向到不同的操作方法(即)當選擇「承包商」時,它應該重定向到AddContractor(),否則它應該重新定向到全時和填充信息。

如何使用單Html.Beginform()來實現它

+0

你不能使用'Html.Beginform()'(被其發送到視圖之前在服務器上生成的HTML,所以它可能永遠只包含初始值)。您需要回發所選選項的值。顯示你的代碼! –

回答

0

好了,你可以這樣做:

上查看:

@{ 
    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"); 
    } 

你可以爲您的項目使用其他視圖和其他信息。我只是代碼示例來重定向動作和模型信息。

希望可以幫到你:)