如何将HTML选择的值传递给控制器​​

时间:2019-10-28 20:32:53

标签: asp.net asp.net-mvc model-view-controller asp.net-mvc-5

我试图将HTML select的值传递给控制器​​,但是我不确定为什么select ID与模型同名,但该方法为什么不捕获值。

  [Authorize]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "CountryId,ProvinceId,CityId")] ads_post ads_post)
    {
        var currUser = currentUser.GetCurrUser();
        ads_post.UserId = currUser.Id;
        ads_post.PostDate = DateTime.Now;
        ads_post.SponsoredType = null;
        ads_post.PriorityType = null;


        if (ModelState.IsValid)
        {
            db.ads_post.Add(ads_post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.PriorityType = new SelectList(db.ads_priority_plan, "Id", "Name");
        ViewBag.SponsoredType = new SelectList(db.ads_sponsored_plan, "Id", "Name");
        ViewBag.CurrencyType = new SelectList(db.ads_currency, "Id", "Name");
        return View(ads_post);
    }

这是HTML

  <div class="row mb-3 mt-5">
                                <div class="col-md-4">
                                    <label><strong>País donde quieres publicar:</strong> <i class="fas fa-list-ul"></i></label>
                                    <select id="CountryId" onchange="getProvices();"></select>
                                </div>
                                <div class="col-md-4">
                                    <label><strong>Provincia:</strong> <i class="fas fa-list-ul"></i></label>
                                    <select id="ProvinceId" onchange="getCities();"></select>
                                </div>
                                <div class="col-md-4">
                                    <label><strong>Ciudad:</strong> <i class="fas fa-list-ul"></i></label>
                                    <select id="CityId"></select>
                                </div>
                            </div>

有人可以帮我这个忙吗? 预先感谢。

1 个答案:

答案 0 :(得分:0)

我自己发现,只需要向HTML添加名称属性即可。