首页 > 解决方案 > 如何将 HTML 选定值传递给控制器

问题描述

我试图将值从我的 HTML 选择传递到控制器,但我不确定为什么尽管选择 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>

有人可以帮我解决这个问题吗?预先感谢。

标签: asp.netasp.net-mvcmodel-view-controllerasp.net-mvc-5

解决方案


我自己发现,我只需要在 HTML 中添加一个 name 属性就可以了。


推荐阅读