如何使用javascript在数据库中保存下拉选择的值?

如何使用javascript在数据库中保存下拉选择的值?,javascript,model-view-controller,dropdown,viewbag,Javascript,Model View Controller,Dropdown,Viewbag,控制器 public ActionResult ItemInsert() { ViewBag.Category = new SelectList(db.CategoryMasters.ToList(), "CategoryId", "CategoryName"); ItemViewModel item = new ItemViewModel(); return Part

控制器

public ActionResult ItemInsert()
        {
            ViewBag.Category = new SelectList(db.CategoryMasters.ToList(), "CategoryId", "CategoryName");
            ItemViewModel item = new ItemViewModel();
            return PartialView(item);
        }
查看

<div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                    @Html.DropDownList("Category", ViewBag.Category as SelectList, new { @id = "catagory" })
                    @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
            </div>
        </div>
<div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               <div hidden>@Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control", id = "categoryid" } })</div>
                    @Html.DropDownList("any value", ViewBag.category as SelectList,"--SELECT--", new { @id = "category" })
                    @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
            </div>
        </div>

@LabelFor(model=>model.CategoryId,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownList(“Category”,ViewBag.Category作为SelectList,new{@id=“catagory”})
@Html.ValidationMessageFor(model=>model.CategoryId,“,new{@class=“text danger”})
如何使用javascript保存下拉列表值?

查看

<div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                    @Html.DropDownList("Category", ViewBag.Category as SelectList, new { @id = "catagory" })
                    @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
            </div>
        </div>
<div class="form-group">
            @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               <div hidden>@Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control", id = "categoryid" } })</div>
                    @Html.DropDownList("any value", ViewBag.category as SelectList,"--SELECT--", new { @id = "category" })
                    @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
            </div>
        </div>

@LabelFor(model=>model.CategoryId,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.CategoryId,new{htmlAttributes=new{@class=“form control”,id=“CategoryId”})
@DropDownList(“任意值”,ViewBag.category作为SelectList,”--SELECT--”,new{@id=“category”})
@Html.ValidationMessageFor(model=>model.CategoryId,“,new{@class=“text danger”})
JS

<script type="text/javascript">
    $(document).ready(function () {
        $("#category").change(function () {
            var value = $(this).val();
            alert(+value)
            $("#categoryid").val(value);
        });
    });
</script>

$(文档).ready(函数(){
$(“#类别”)。更改(功能(){
var值=$(this.val();
警报(+值)
$(“#categoryid”).val(价值);
});
});