无法将Dropdownlist Selected Value传递给Action Method

时间:2013-11-16 09:52:42

标签: asp.net-mvc razor parameter-passing html.dropdownlistfor selectlist

我已经将ModelView用于静态Dropdownlist值,并尝试在MVC4中从Razor调用print方法。但是,我无法将选定的Dropdownlist值传递给teh Action方法,而我可以在输入字段中传递其他参数。你能告诉我哪里弄错了吗?


ApplicantViewModel:

public class ApplicantViewModel
{
    public IEnumerable<Applicant> Applicants { get; set; }

    //Codes for Dropdownlist values
    public string SelectedValue { get; set; }
    public IEnumerable<SelectListItem> Values
    {
        get
        {
            return new[]
            {
                new SelectListItem { Value = "pdf", Text = "Pdf" },
                new SelectListItem { Value = "excel", Text = "Excel" },
                new SelectListItem { Value = "word", Text = "Word" }
            };
        }
    }
}


ApplicantController:

public ViewResult Reporting()
{
    var model = new ApplicantViewModel(); 
    return View(model);
}

public ActionResult RenderReport(string type, string name, string fileName, string dataSource, string table, string filter)
{
    //Codes for rendering report
    ...
}


Reporting.cshtml:

@model MyProject.Models.ApplicantViewModel

@using (Html.BeginForm())
{ 
 <div>
    @Html.DropDownListFor(model => model.SelectedValue, Model.Values, "-- select an option --", new { type=Model.SelectedValue, name = "type", id = "type"})

    <input type="button" title="Render Report" value="P" onclick="'location.href=@Url.Action("RenderReport", "Applicant", 
    new { " 
     //type="pdf", //if I assign type value at here no problem. But I want the value to be assined by Dropdownlist
     type=Model.SelectedValue, //It pass SelectedValue = null to the Controller
     name="Report1",            
     fileName="Applicant list",
     dataSource="ApplicantDataset",
     table="ApplicantsView",
     filter="David"id = item.ID }) "/>    
</div>
    }    
}

2 个答案:

答案 0 :(得分:4)

<强> ApplicantViewModel:

`public class StaticList {

    public string type { get; set; }

    public List<SelectListItem> DropdownList { 
        get {
                return new List<SelectListItem> 
                {
                    new SelectListItem { Selected = false,Value = "pdf", Text = "Pdf" },
                    new SelectListItem { Selected = false,Value = "excel", Text = "Excel" },
                    new SelectListItem { Selected = false,Value = "word", Text = "Word" }
                };
            }
    }
}`

<强> ApplicantController:

 public  ActionResult Test(StaticList list)
    {
        list.type = "pdf";
        return View(list);
    }

<强> Reporting.cshtml:

@Html.DropDownListFor(model => model.type, Model.DropdownList)

一切都适合我。

答案 1 :(得分:2)

您的下拉列表名称必须等于您的操作中的参数名称。

编辑: 我重复你的代码,如果RenderReport有string SelectedValue参数

,一切正常
public ActionResult RenderReport(string SelectedValue, string name, string fileName, string dataSource, string table, string filter)
{
    //Codes for rendering report
    ...
}

或使用 @ Html.DropDownList 并为DropDownList设置自定义名称

 @Html.DropDownList("type", Model.Values, "-- select an option --", new {id = "type"})