我需要在控制器中获取Select值
变量“”中的strDDLValue
我尝试这样做,但是不起作用
[HttpPost]
public async Task<IActionResult> Create(AddItemViewModel model, IFormCollection form)
{
if(ModelState.IsValid)
{
string strDDLValue = Request.Form["selectCategory"].ToString();
html
<select class="browser-default custom-select" id="selectCategory">
</select>
自动完成
<script type="text/javascript">
$.ajax({
type: 'GET',
url: '/Item/GetItemCategories',
dataType: 'json',
success: function (category) {
$.each(category, function (key, value) {
$('#selectCategory')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
}
});
</script>
答案 0 :(得分:1)
检查是否在ajax的回调中正确填充了选择标签帮助器。这是工作示例:
GetCountry的控制器和ajax中的成功功能
public JsonResult GetCountry()
{
var country = _context.Country.ToList();
return Json(new SelectList(country,"Id","CountryName"));
}
success: function (result) {
$("#selectCountry").empty();
$.each(result, function (i, item) {
$("#selectCountry").append('<option value="' + item.value + '"> ' + item.text + ' </option>');
});
指定要填充的选择标签助手的name
属性
<select class="browser-default custom-select" name="selectCountry" id="selectCountry"> </select>
表单数据在请求中另存为键值对,您可以像以下所示获得selectCountry的值
var dict = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
//In that case, you could iterate over your dictionary or you can access values directly:
var ddl = dict["selectCountry"];