Could not pass Dropdownlist Selected Value to Action Method

房东的猫 提交于 2019-12-11 20:12:04

问题


I have used a ModelView for static Dropdownlist values and tried to call print method from Razor in MVC4. However, I cannot pass the selected Dropdownlist value to teh Action method while I could pass the other parameters in input field. Could you please tell me where I made mistake?


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

回答1:


Name you dropdown list must be equals parameter name in your Action.

Edit: I repeat you code, all work fine if RenderReport have string SelectedValue parameter

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

or Use @Html.DropDownList and set custom Name for DropDownList

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



回答2:


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)

Everything works fine for me.



来源:https://stackoverflow.com/questions/20017037/could-not-pass-dropdownlist-selected-value-to-action-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!