将值从Kendo DDL传递到ActionLink / ActionImage而不提交值

时间:2013-08-28 18:36:34

标签: asp.net-mvc razor kendo-ui actionlink

VS' 12 KendoUI InternetApplication Template C#asp.net EF Code First

我有2个下拉列表页面1页面使用DropDownList For Posting值到ModelView,另一个只是按ID返回。

Qeustion

基本上我想用一个用户可以选择的DDL留在视图中。在这个DDL的左边有一个Actionlink,它应该能够使用所选参数进入新的ACtion。但我的模型值始终为空。如何传递这个值?

的DropDownList

                <label for="Prospects">Prospect:</label>
                @(Html.Kendo().DropDownListFor(m=>m.Prospects)
                      //.Name("clients")
                      .HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
                      .OptionLabel("Select Prospect...")
                      .DataTextField("ProspectName")
                      .DataValueField("ProspectID")
                      .DataSource(source => {
                           source.Read(read => {
                               read.Action("GetCascadeProspects", "AddCCCTRST");
                           });
                      })
                  )

ActionLink的

@Html.ActionImage("ADD", "Prospect", new { id=Model.Prospects.FirstOrDefault() } , "~/Images/Navigation/Add.PNG", "Add")

..., new { id=Model.Prospects.FirstOrDefault() } ,...,...) 
// the Model is null here, how can i pass my value to this actionlink / actionimage?

(ActionImage来自This Post)如果需要在此处发布代码请告知。 (自定义ActionImage工作,我只能传递值....)

  

更新模型

public class ViewModelCCTRST
{
    public string Clients { get; set; }
    public IEnumerable<dbClient> AvailableClients { get; set; }
    public string Prospects { get; set; }
    public IEnumerable<dbProspect> AvailableProspects { get; set; }
    public string Countys { get; set; }
    public IEnumerable<dbCounty> AvailableCounties { get; set; }
    public string TownShips { get; set; }
    public IEnumerable<dbTownShip> AvailableTownShip { get; set; }
    public string Ranges { get; set; }
    public IEnumerable<dbRange> AvailableRanges { get; set; }
    public string Sections { get; set; }
    public IEnumerable<dbSection> AvailableSection { get; set; }
    public string Tracts { get; set; }
    public IEnumerable<dbTract> AvailableTracts { get; set; }
}
  

控制器* JsonResult GET方法和发布请求

    public JsonResult GetCascadeTracts(int sections)
    {
        var tract = db.Tracts.AsQueryable();

        if (0 != sections)
        {
            tract = tract.Where(o => o.SectionID == sections);
        }

        return Json(tract.Select(o => new { TractID = o.TractID, TractName = o.TractNumber }),    JsonRequestBehavior.AllowGet);
    }

    [HttpPost]
    public ActionResult Index(ViewModelCCTRST model)
    {

        if (ModelState.IsValid)
        {

            //string clt = model.Clients;
            string pro = model.Prospects;
            string cnt = model.Countys;
            string twn = model.TownShips;
            string rng = model.Ranges;
            string sct = model.Sections;
            string trt = model.Tracts;

            return Content(cnt + twn + rng + sct + trt);
        }
        else
        {
            return Content("");
        }
    }

**正如下面讨论的那样,我的Post方法中有值,但我的View中没有任何值(这是我希望能够使用我的值的地方) Actionlinks无需刷新页面)如果我将Model.(w/e the data is)绑定到action links,他们会给我null reference

2 个答案:

答案 0 :(得分:1)

  1. 将ID添加到Actionlink

    @Html.ActionLink("Add", "Create", new { controller = "Client"}, new {id="AddClient"}) 
    
  2. 添加以捕捉onclick事件

     $('#AddClient').click(function (e) {
        var val = $("#Clients").val();
        var href = "/Client/Create/" + val;
        this.href = ""; //clears out old href for reuse
        this.href = href; //changes href value to currently slected dropdown value
    });
    
  3. srcs

答案 1 :(得分:0)

您可以使用kendo template参考:Kendo DropDownList / Customizing templates

@(Html.Kendo().DropDownList()
      .Name("customers")
      .HtmlAttributes(new { style = "width: 400px" })
      .DataTextField("ContactName")
      .DataValueField("CustomerID")
      .DataSource(source =>
      {
          source.Read(read =>
          {
              read.Action("GetCustomers", "Home");
          });
      })
      .Height(300)
      .Template("<img src=\"" + Url.Content("~/Content/web/Customers/") +  "${data.CustomerID}.jpg\" alt=\"${data.CustomerID}\" />" +
                        "<dl>" +
                            "<dt>Contact:</dt><dd>${ data.ContactName }</dd>" +
                            "<dt>Company:</dt><dd>${ data.CompanyName }</dd>" +
                        "</dl>")
)

<强>更新

由于尚未将Name指定给kendo控件,因此您将获得空值。如果不这样做,控件值将不会绑定到您的模型。

@(Html.Kendo().DropDownListFor(m=>m.Prospects)
                  .Name("Prospects")         // assign appropriate name
                  .HtmlAttributes(new { style = "width:300px"}) //, id = "clients"})
                  .OptionLabel("Select Prospect...")
                  .DataTextField("ProspectName")
                  .DataValueField("ProspectID")
                  .DataSource(source => {
                       source.Read(read => {
                           read.Action("GetCascadeProspects", "AddCCCTRST");
                       });
                  })
              )