通过@ Html.DropDownList选择使用@ Html.ActionLink与视图模型到控制器 [英] Pass @Html.DropDownList selection using @Html.ActionLink with ViewModel to Controller

查看:320
本文介绍了通过@ Html.DropDownList选择使用@ Html.ActionLink与视图模型到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过帖子阅读关于这个两天了,还没有想出答案。我想捕捉我的模型视图内DropDownList的选择,传递给@ Html.ActionLink,将其发回给控制器内的具体行动。

I've been reading through posts for two days regarding this, and still have not figured out the answer. I want to capture the DropDownList selection within my ModelView, pass this to a @Html.ActionLink, which will send it back to a specific Action within the Controller.

我的视图模型:

    public class ViewModelShipments
    {
        public ViewModelShipments()
        {
            tblShipments = new tblShipments();
        }
        public tblShipments tblShipments;
        public IEnumerable<SelectListItem> ShipmentIDsList;
        public string SelectedShipmentID; 
    }

我的控制器:

public ActionResult SelShipment(string SelectedShipmentID)//for ShipmentID change via DropDownList
        {
            int id = db.tblShipments.Max(p => p.ShipmentID); // find last ShipmentID
            if (SelectedShipmentID != null)
            {
                id = Int32.Parse(SelectedShipmentID);//convert text to int
            }

我的观点:

            @Html.DropDownListFor(expression: model=>model.SelectedShipmentID,selectList: Model.ShipmentIDsList) @* render DropDownList object*@
            @Model.SelectedShipmentID @* display value of SelectedShipmentID *@

            <!-- Upon Click, send selected ID to controller --> 
            <!-- the ActionLink is working, but the routeValues: always contain NULL -->
            @Html.ActionLink(linkText: "Submit", actionName: "SelShipment", controllerName: "Shipment", routeValues: Model.SelectedShipmentID, htmlAttributes: null)

为什么ActionLink的(...,routeValues​​:Model.SelectedShipmentID,...)总是返回NULL到控制器?该Model.SelectedShipmentID不会更新与DropDownList中选定的ID。请帮助因为我在这的时间不多了。

Why does the ActionLink(..., routeValues: Model.SelectedShipmentID,...) always return NULL to the Controller ? The Model.SelectedShipmentID is not updated with the DropDownList selected id. Please help as I'm running out of time on this.

推荐答案

剃须刀code在服务器上的发送到视图之前分析的,所以你的路线参数的值将是<$ C的初始值$ C> SelectedShipmentID 。从DropDownList中选择一个值不会改变你已经呈现的网址。

Razor code is parsed on the server before its sent to the view, so the value of your route parameter will be the initial value of SelectedShipmentID. Selecting a value from a dropdownlist does not change the url you have already rendered.

您可以使用JavaScript / jQuery来处理DropDownList的 .change()事件(或链接。点击()事件)来更新网址,但是更好的方式来处理,这是通过使用一种形式,使一个GET到控制器方法

You could use javascript/jquery to handle the dropdownlist's .change() event (or the links .click() event) to update the url, however the better way to handle this is by using a form that makes a GET to your controller method

@model ViewModelShipments
@using (Html.BeginForm("SelShipment", "Shipment", FormMethod.Get))
{
  @Html.DropDownListFor(m => m.SelectedShipmentID, Model.ShipmentIDsList, "-Please select-")
  <input type="submit" />
}

请注意的最后一个参数 DropDownListFor()添加标签的选项让你回发,但不知道如果这是适合您。

Note the last parameter of DropDownListFor() add a label option allowing you to post back null but not sure if this is appropriate for you.

由于您绑定到一个值,该值 INT 那么这两个模型属性和方法的参数应该是 INT?没有字符串。在additiona这样,当一个有效的值传递给方法,你不会做不必要的数据库调用你应该改变逻辑控制器。

Since your binding to a value which is int then both your model property and method parameter should be int? not string. In additiona you should change the logic in the controller so that you are not making unnecessary database calls when a valid value is passed to the method.

public ActionResult SelShipment(int? SelectedShipmentID)
{
  if (!SelectedShipmentID.HasValue)
  {
    // only necessary to call database if SelectedShipmentID is null
    SelectedShipmentID = db.tblShipments.Max(p => p.ShipmentID)
  }
  ....
}

边注:从您的视图模型的属性,我假设你想在视图中显示的一些数据的基础上选择的 ShipmentID 的值。如果是这样,你应该考虑使用AJAX后选定的值来返回基于价值的 tblShipments 数据的控制方法,无论是作为一个局部视图或JSON和更新当前页面,而不是做一个完整的页面,每次刷新。

Side note: From your view model properties, I assume you wanting to display some data in the view based on the value of the selected ShipmentID. If so you should consider using ajax to post the selected value to a controller method that returns the tblShipments data based on the value, either as a partial view or as json, and update the current page, rather than doing a complete page refresh each time.

这篇关于通过@ Html.DropDownList选择使用@ Html.ActionLink与视图模型到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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