简体   繁体   English

控制器中的MVC3自动完成和POST方法

[英]MVC3 Autocomplete and POST Method in Controller

I got a MVC Razor View page as a form for people to enter details and submit to a database. 我得到了一个MVC Razor View页面,作为人们输入详细信息并提交到数据库的一种形式。 It works using dropdownlist but I want to add a autocomplete textbox to the form. 它使用dropdownlist起作用,但是我想向表单添加自动完成文本框。 This works fine however when posting back to the POST controller it sets the ID of the person to 0 always. 这可以正常工作,但是当发回到POST控制器时,它将人员的ID始终设置为0。 Any ideas? 有任何想法吗?

Thanks 谢谢

Javascript: Javascript:

 script type="text/javascript">

   $(document).ready(function () {
        $("#PersonName").autocomplete({
            source: '@Url.Action("AutocompleteSuggestions")',
            minLength: 2,

            select: function (event, ui) {
                if (ui.item) {
                    $("#PersonName").val(ui.item.label);
                    $("#PersonId").val(ui.item.value);
                }
            }
        });
    });
</script>

Controller: 控制器:

 [HttpPost]
    public ActionResult Create(MappingModel view)
    {

        if (ModelState.IsValid)
        {
            var entity = new PersonContempancy();
            entity.PersonId = view.PersonId;
            entity.FrameworkId = view.FrameworkId;
            entity.ContempancyCategoryId = view.ContempancyCategoryId;
            entity.ContempancyId = view.ContempancyId;
            entity.ContempancyLevelId = view.ContempancyLevelId;
            entity.FrameworkLevelId = view.FrameworkLevelId;
            db.PersonContempancies.Add(entity);
            db.SaveChanges();

            return RedirectToAction("Index");  
        }



        ViewBag.Contepancies = db.Contempancies.ToList();
        ViewBag.ContempancyCategory = db.ContempancyCategories.ToList();
        ViewBag.ContempancyLevel = db.ContempancyLevels.ToList();
        ViewBag.FrameworkLevel = db.FrameworkLevels.ToList();
        ViewBag.Person = db.People.ToList();
        ViewBag.Framework = db.Frameworks.ToList();



        return View();
    }

  public ActionResult AutocompleteSuggestions(string term)
    {

        var namelist = db.People.Where(c => c.PersonName.Contains(term)).Select(c => new { value = c.PersonId, label = c.PersonName}).Distinct().Take(10);

        return Json(namelist.ToArray(), JsonRequestBehavior.AllowGet);

    }

Looking at your AutoCompleteSuggestions code it looks like you are only sending back the person name, not the person's id, when returning your JSON. 查看您的AutoCompleteSuggestions代码,看起来您在返回JSON时只发送了人名,而不是人的ID。 If i'm understanding correctly you need to return both the name and the ID and set the person id via JQuery in the select function in your autocomplete function. 如果我正确理解,则需要返回名称和ID,并通过JQuery在自动完成功能的select函数中设置人员ID。 It should look (roughly) something like this: 它看起来应该(大致)如下所示:

var namelist = (from c in db.People where c.PersonName.Contains(term) select new { c.PersonName, c.PersonID}).Distinct().Take(10).ToArray();
return Json(namelist.ToArray(), JsonRequestBehavior.AllowGet);

and

     $("#searchTerm").val(ui.item.value.PersonName);
     $("#PersonID").val(ui.item.value.PersonID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM