不能错过的DropDownList选中的值给操作方法 [英] Could not pass Dropdownlist Selected Value to Action Method

查看:115
本文介绍了不能错过的DropDownList选中的值给操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用静态DROPDOWNLIST值的模型视图并试图从剃刀在MVC4调用print方法。但是,我不能选择DROPDOWNLIST值传递给德法行动,而我可以通过输入字段的其它参数。你能告诉我在哪里出错了?


ApplicantViewModel:

 公共类ApplicantViewModel
{
    公共IEnumerable的<申请人GT;申请人{搞定;组; }    // codeS为DROPDOWNLIST值
    公共字符串的SelectedValue {搞定;组; }
    公共IEnumerable的< SelectListItem>值
    {
        得到
        {
            返回新的[]
            {
                新SelectListItem {值=PDF,文本=PDF},
                新SelectListItem {值=创先争优,文本=创先争优},
                新SelectListItem {值=字,文本=字}
            };
        }
    }
}


ApplicantController:

 公开的ViewResult报告()
{
    VAR模型=新ApplicantViewModel();
    返回查看(模型);
}公众的ActionResult RenderReport(字符串类型,字符串名称,字符串文件名,字符串数据源,字符串表,字符串过滤器)
{
    // codeS渲染报告
    ...
}



Reporting.cshtml:

  @model MyProject.Models.ApplicantViewModel@using(Html.BeginForm())
{
 < D​​IV>
    @ Html.DropDownListFor(型号=> model.SelectedValue,Model.Values​​ - 选择一个选项 - 新{类型= Model.SelectedValue,名称=类型,ID =输入})    <输入类型=按钮称号=渲染报告VALUE =P的onclick ='location.href=@Url.Action(RenderReport,申请人,
    新{
     // TYPE =PDF,//如果我在这里没问题的指定类型值。但我想通过DROPDOWNLIST被assined值
     键入= Model.SelectedValue,//它传递的SelectedValue = null来控制器
     NAME =报告1
     文件名=申请表,
     数据源=ApplicantDataset
     表=ApplicantsView
     过滤器=大卫ID = item.ID})/>
< / DIV>
    }
}


解决方案

名称您下拉列表必须在你的行动等于参数名。

编辑:
我再说一遍,你code,所有的正常工作,如果有RenderReport 字符串的SelectedValue 参数

 公众的ActionResult RenderReport(字符串的SelectedValue,字符串名称,字符串文件名,字符串数据源,字符串表,字符串过滤器)
{
    // codeS渲染报告
    ...
}

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

  @ Html.DropDownList(类型,Model.Values​​ - 选择一个选项 - 新{ID =输入})

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

解决方案

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

这篇关于不能错过的DropDownList选中的值给操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆