如何将我的下拉选择值保存在数据库中....使用 javascript?
how to save my dropdown selected values in database....using javascript?
控制器
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>
如何使用 javascript 保存下拉值?
查看
<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>
JS
<script type="text/javascript">
$(document).ready(function () {
$("#category").change(function () {
var value = $(this).val();
alert(+value)
$("#categoryid").val(value);
});
});
</script>
控制器
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>
如何使用 javascript 保存下拉值?
查看
<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>
JS
<script type="text/javascript">
$(document).ready(function () {
$("#category").change(function () {
var value = $(this).val();
alert(+value)
$("#categoryid").val(value);
});
});
</script>