0
我在我的视图模型中有以下字段,我试图在自动完成中使用它,如果值不存在,则为表添加新值。使用IEnumerable值自动完成结果始终为空
永远传递给控制器的值始终是null
。
我认为这个问题是这里
“@ Html.TextBoxFor(米=> m.Genres,新的{@class = “形式控制”,@ ID = “术语”}) “
我需要获得。名称属性。我不想改变我的虚拟机。
public class LectureFormViewModel
{
public int Id { get; set; }
//Values
public byte Genre { get; set; }
public IEnumerable<Genre> Genres { get; set; }
}
public JsonResult GetGenresName(string term)
{
var genre = _context.Genres
.Where(gn => term == null ||
gn.Name.ToLower().Contains(term.ToLower())).Select(x => new
{ id = x.Id, value = x.Name }).Distinct().ToList();
return Json(genre, JsonRequestBehavior.AllowGet);
}
@using (Html.BeginForm("Create", "VoosUp", FormMethod.Post, new {enctype = "multipart/form-data", id = "RestoForm"}))
{
//values
<div class="form-group">
@Html.LabelFor(model => Model.Genre, new {@class = "control-label"})
@Html.TextBoxFor(m => m.Genres, new {@class = "form-control", @id = "term"})
@Html.ValidationMessageFor(m => m.Genres)
</div>
<input type="button" class="btn btn-primary btn-lg pull-right" onclick="GetLocation()" value="Finish"/>
}
@section scripts
{
@Scripts.Render("~/bundles/jqueryval")
<script>
$("#term").autocomplete({
source: function(request, response) {
$.ajax({
url: '@Url.Action("GetGenresName", "VoosUp")',
data: "{'GetGenresName': '" + request.term + "' }",
dataType: 'json',
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function(data) { return data; },
success: function(data) {
console.log(term),
console.debug();
response($.map(data,
function(item) {
return {
label: item.value,
value: item.value,
id: item.id
}
}));
}
});
},
minLength: 2
});
</script>
@ M.kazemAkhgary yes真正的,但我的空值是在公共JsonResult GetGenresName(字符串术语)/ /空的问题在这里:@ Html.TextBoxFor(m => m.Genres它应该得到的名称值 –