asp.net - 如何将 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>
有人可以帮我解决这个问题吗?预先感谢。
解决方案
我自己发现,我只需要在 HTML 中添加一个 name 属性就可以了。
推荐阅读
- php - 如何用撇号爆炸数组
- c# - 另一个进程使用的文件 - StreamWriter
- angular - 如何从grantOfflineAccess返回的response_code中获取refresh_token
- sql - 如何在逗号分隔值上使用 sql where 条件
- c# - QnAMaker 中的主动学习建议
- azure - Azure:将白名单证书上传到应用程序网关
- swift - 是什么导致了 Swift 中的“无法转换类型”错误?
- swift - 如何使用 Swift 在 Mac 上的运行时填充选项并显示 NSPopUpButton?
- octobercms - OctoberCms 上的 Robots.txt 和 Sitemap.xml
- python - Pybind - 使用指向派生类的共享指针调用函数