【发布时间】:2017-05-10 08:53:19
【问题描述】:
我的视图模型中有以下字段,我正在尝试在自动完成中使用它,如果值不存在,请向表中添加新值。
传递给控制器的值总是null。
我认为问题出在这里
"@Html.TextBoxFor(m=>m.Genres,new { @class= "form-control" ,@id="term" }) "
我需要获得 .Name 属性。我不想改变我的虚拟机。
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 是的,这是真的,但我的 null 值在 public JsonResult GetGenresName(string term)//Nulll 问题就在这里:@Html.TextBoxFor(m=>m.Genres 它应该得到名称值
标签: c# jquery asp.net-mvc autocomplete