基于DropDownList的值和模型调用Action方法

Calling an Action Method based on the value of DropDownList and the model

本文关键字:调用 Action 方法 模型 DropDownList 基于      更新时间:2023-09-26

我的视图中有一个下拉列表。我需要让用户从下拉列表中选择一个值,然后单击按钮/ActionLink来调用同一控制器中的另一个操作方法。需要传递到新ActionMethod的值是从下拉列表中选择的值的ID,以及传递到视图中的模型的ID。模型和Dropdownlist不会以任何方式链接在一起。

我尝试过onchnage = document.location.href来设置动作方法的路径,并将单个值传递给动作方法。但document.location.href的问题是,它将url附加到现有url上,这是不受欢迎的;也就是说,最终的url原来是localhost:port/controller1/action1/controller1/action2,它应该是简单的localhost:port/controller1/action2

我正在寻找一种不使用javascript的方法,因为我已经尝试过了

视图中的代码

@using (Html.BeginForm("Copy","SetValues",FormMethod.Post))
{
    <p>
        @Html.DropDownList("OptionValueID", null, "Select")
        <input type="submit" value="Copy" />
        //This is the preferable method though
        @*@Html.ActionLink("Copy", "Copy", "SetValues", new { @OptionValueID = @ViewBag.id,@CopyID = CopyDDL.SelectedValue},null)*@
    </p>
}

复制函数将采用两个参数:Id of the selected itemID that is being passed through ViewBag.id

View返回的View将是不同的View

我尝试过的JavaScript

<script language="javascript" type="text/javascript">
    function copy(_OptionValueID)
    {
        var url = "/SetValues/Copy";
        $.ajax({
            url: url,
            data: { copyid: _OptionValueID},
            type: "POST",
            success: function (data) { }

        });
        response();
    }
</script>

它根本不评估。

调用此视图的操作方法

public ActionResult Index(int id)
{
        var ov = db.OptionValue.Include(x => x.Option).FirstOrDefault(x => x.OptionValueID == id);
        var opid = ov.OptionID;
        var op = db.Option.Include(x => x.TechnicalCharacteristic).FirstOrDefault(x => x.OptionID == opid);
        var tcid = op.TechnicalCharacteristicID;
        var tc = db.TechnicalCharacteristic.Include(x => x.TcSets).FirstOrDefault(x => x.TechnicalCharacteristicID == tcid);
        var tcset = tc.TcSets;
        var opv = db.OptionValue.FirstOrDefault(x => x.OptionValueID == id);
        ViewBag.OptionValue = opv.OptionVal;
        ViewBag.Option = opv.Option.OptionName;
        ViewBag.Lsystem = opv.Option.Lsystem.LsystemName;
        ViewBag.FamilyName = opv.Option.Lsystem.LsystemFamily.FamilyName;
        ViewBag.OptionValID = id;
        ViewBag.OptionID = opv.OptionID;
        var setValue = db.SetValue.Where(x=>x.OptionValueID==id).OrderBy(x=>x.TcSet.SetName);
        ViewBag.OptionValueID = new SelectList(db.OptionValue.Where(x=>x.OptionID==opid), "OptionValueID", "OptionVal");
        return View(setValue.ToList());
}

我已经检查了与此相关的大多数问题,但没有一个问题会在不使用模型的情况下传递两个参数。

更新:使其更加清晰

 public ActionResult copy(int OptionValueID,int CopyID)
 {
     //do Something
     return View("Error");
 }

以上是复制方法

OptionValueID = ViewBag.OptionValID //Got from the Action Method Index of SetValues
CopyID = Value from the DropDownlist

根据答案编辑

@using (Html.BeginForm("Copy","SetValues",FormMethod.Post))
{
    <p>
        @Html.DropDownList("CopyID", null, "Select")
        <button type="submit" id="Copy" data-id="@ViewBag.OptionValID"> Copy </button>
    </p>
}

现在页面正在被重定向,但是no参数正在被传递。我应该添加路线评估吗?

没有javascript就无法实现。ActionLink()方法在发送到客户端之前会在服务器上进行解析,因此任何路由值都是控制器中的初始值,而不是用户在视图中创建的任何编辑值。为了响应客户端事件,您需要javascript。

您可以使用ajax将值发布到服务器方法。

包括一个按钮并处理其点击事件

<button type="button" id="Copy" data-id="@ViewBag.id">Copy</button>

编写脚本

var url = '@Url.Action("Copy", "SetValues")';
$('#Copy").click(function() {
  var optionID = $(this).data('id');
  var copyID = $('#OptionValueID').val();
  $.get(url, { OptionValueID: optionID, copyID : CopyID }, function(response) {
    // do something with the response
  });
});

或者,如果您想重定向,则用替换$.get()

location.href = url + '?OptionValueID=' + optionID + '&CopyID=' + copyID;

编辑

根据修改后的问题和评论,如果你想发布和重定向,不需要任何javascript或链接。dropdownlist需要是@Html.DropDownList("CopyID", null, "Select"),以便其所选值绑定到方法参数int CopyID,并且由于OptionValueID未被编辑,因此将其值添加为形式的路由参数

@using (Html.BeginForm("Copy", "SetValues", new { OptionValueID = ViewBag.OptionID }, FormMethod.Post))

或添加值的隐藏输入

<input type="hidden" name="OptionValueID" value="@ViewBag.OptionID" />