2017-06-13 55 views
0

想在控制器下拉列表中選擇值後點擊提交按鈕無法獲得選擇的值當點擊在asp.net mvc5提交按鈕

查看部分: -

@Html.DropDownListFor(m => m.ExportFormat, new List<SelectListItem>() 
{ 
    new SelectListItem(){Value="PDF", Text = "PDF"}, 
    new SelectListItem(){Value=".CSV", Text = ".CSV"}, 
    new SelectListItem(){Value="Excel", Text = "Excel"} 
}, "Select Format", new { @class = "form-control editable" }) 

<input type="submit" value="Export file" class="btn btn-primary" name="Command" /> 

控制器部分: -

[AllowAnonymous] 
public ActionResult Reports(ReportsModel model,string Command) 
{ 
    if(Command=="PDF") 
    { 

    } 
    else if(Command==".CSV") 
    { 

    } 
    else if(Command=="Excel") 
    { 

    } 
} 
+0

是否'ReportsModel'包含一個屬性'ExportFormat'?如果是這樣,它將與選定的選項綁定。您的提交按鈕沒有'value'屬性,因此它不會發布值 - 'Command'的值將始終爲'null'。但是,爲什麼你要使用它來獲得'ExportFormat'屬性的值呢? –

回答

0

嗨如果您想要獲取下拉列表控件的值,請在控制器操作方法中將下拉列表控件的名稱作爲操作方法中的參數。

例如:

[AllowAnonymous] 
public ActionResult Reports(ReportsModel model,string ExportFormat) 
{ 

} 

希望在你的代碼,在下拉列表控件的名稱是exportformat,請覈實在查看源代碼,並給它作爲一個參數,那麼你當然應該得到它。

感謝

KARTHIK

0

這不是要問一個好問題的方法!但是你可以用下面的代碼段更新您的@Html.DropDownListFor();

@Html.DropDownListFor(m => m.ExportFormat, new List<SelectListItem>() 
    { 
    new SelectListItem(){Value="PDF", Text = "PDF"}, 
    new SelectListItem(){Value=".CSV", Text = ".CSV"}, 
    new SelectListItem(){Value="Excel", Text = "Excel"} 
    },"Model.ExportFormat", "Select Format", new { @class = "form-control 
     editable", onchange = "this.form.submit()" }) 

這將是更好的,如果你把控制器的動作,你所提交的變化形式!

+0

沒有這樣的超負荷! –