使用选定的值表单Dropdownlist更新HTML.HiddenFor值

时间:2016-02-17 10:01:11

标签: javascript jquery html asp.net-mvc-5 onchange

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

EditorTemplate

   <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来完成,但是到目前为止我尝试过的所有内容都会失败?

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

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

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

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