使用下拉列表中选择的值更新 HTML.HiddenFor 值

Update HTML.HiddenFor value with selected value form Dropdownlist

我有一个简单的销售信息形式,用户可以编辑评论和原因,然后使用 SaveChanges() 函数将数据提交回数据库。 我需要更新一个包含 ID 号的字段。我有一个简单的下拉列表(由 viewbag 填充),当用户进行更改时,我想更新 hiddenfor 字段中的 ID 号,以便它可以在保存时回发。 我的代码如下:

编辑器模板

   <tr>
@Html.HiddenFor(modelItem => modelItem.ID)
@Html.HiddenFor(modelItem => modelItem.tblTaggingGroupID)
@Html.HiddenFor(modelItem => modelItem.FinYear)
@Html.HiddenFor(modelItem => modelItem.FinWeek)
@Html.HiddenFor(modelItem => modelItem.WeekDay)
@Html.HiddenFor(modelItem => modelItem.Segment)
@Html.HiddenFor(modelItem => modelItem.NetSales)
@Html.HiddenFor(modelItem => modelItem.SwellSales)
@Html.HiddenFor(modelItem => modelItem.SpikeSales)
@Html.HiddenFor(modelItem => modelItem.BaseSales)
@Html.HiddenFor(modelItem => modelItem.tblTaggingReasonID)
<td>
    @switch (Model.WeekDay)
    {
        case 1:
            @:Sunday
            break;
        case 2:
            @:Monday
            break;
        case 3:
            @:Tuesday
            break;
        case 4:
            @:Wednesday
            break;
        case 5:
            @:Thursday
            break;
        case 6:
            @:Friday
            break;
        case 7:
            @:Saturday
            break;
        default:
            break;
    }
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.Segment)
</td>

<td>
    @Html.DisplayFor(modelItem => modelItem.NetSales)
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.SwellSales)
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.SpikeSales)
</td>
<td>
    @switch (Model.Recurring)
    {
        case true:
            Model.Recurring.ToString();
            break;
        case false:
            Model.Recurring.ToString();
            break;
        default:
            break;
    }
    @Html.EditorFor(modelItem => modelItem.Recurring)
</td>

<td>@Html.DropDownListFor(modelItem => modelItem.tblTaggingReasonID, (SelectList)ViewBag.Reasons, "Please Select")</td>
<td>
    @Html.EditorFor(modelItem => modelItem.Comment, new { @class = "Commenttxt" })
</td>

控制器:

        public ActionResult Index()
    {
        var SelectedID = Convert.ToInt32(Session["_SessionSelectGroupID"]);
        var SelectedYear = Convert.ToInt32(Session["_SessionSelectFinYear"]);
        var SelectedWeek = Convert.ToInt32(Session["_SessionSelectFinWeek"]);
        var tblTaggingGroupInstances = db.tblTaggingGroupInstances.Include(t => t.tblTaggingGroup);

         ViewBag.Reasons = new SelectList(db.tblTaggingReasons,"ID","UpliftReason");

        return View(tblTaggingGroupInstances.Where(t => t.tblTaggingGroupID == SelectedID && t.FinYear == SelectedYear && t.FinWeek == SelectedWeek).ToList());
    }
    [HttpPost]
    public ActionResult Index(List<tblTaggingGroupInstance> model)
    {
        foreach (var record in model)
        {
            db.Entry(record).State = EntityState.Modified;
        }
        db.SaveChanges();
        return RedirectToAction("Details");
    }

我假设这可以通过一些简单的 JQuery 或 Javascript 来完成,但是,到目前为止我尝试过的一切都失败了吗?

使用 #Reasons 附加 change() 事件并更新 #tblTaggingReasonID 值。如图:-

$("#Reasons").on("change",function(){
   $("#tblTaggingReasonID").val(this.value)
});

不要忘记在您的页面中包含 jQuery 文件。

无需使用javascript更新隐藏输入。而是直接绑定到 属性

@Html.DropDownList(m => m.tblTaggingReasonID, (SelectList)ViewBag.Reasons, "Please Select")

并从视图中删除隐藏的输入。