我已經嘗試了三天,但我無法找出錯誤,因為代碼是正確的,但它的實現不是我想要的。 我正在使用三個表“州”、“城市”和“捐贈者”,我希望捐贈者在選擇州時選擇其州和城市,它顯示指定的城市,但在保存其信息時,城市 ID 不會保存為圖像中的那樣在最后。我首先使用 ap.net mvc 5 代碼。任何人都可以幫助并告訴我如何解決這個問題 以下是我的模型、控制器和視圖:@model WebApplication6.Models.Donator@{ Layout = "~/Views/Shared/_Layout.cshtml";}<!DOCTYPE html><html><head> <meta name="viewport" content="width=device-width" /> <title>Create</title></head><body> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") <script type="text/jscript"> $(function () { $('#State').change(function () { $.getJSON('/Donators/Citylist/' + $('#State').val(), function (data) { var items = '<option>Select a City</option>'; $.each(data, function (i, city) { items += "<option value='" + city.Value + "'>" + city.Text + "</option>"; }); $('#city').html(items); }); }); }); </script> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>Donator</h4> <hr /> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) </div> </div>
1 回答

holdtom
TA貢獻1805條經驗 獲得超10個贊
在您的 POST 創建控制器操作中,您沒有綁定 CityId。您正在綁定 CountryId,這是一個拼寫錯誤嗎?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,gender,Age,BloodType,CountryId,StateId,CityId")] Donator donator)
{
// add cityId to the bind parameters above
...
}
您需要將 html 幫助程序上的 City 更新為 CityId。
// please update first string parameter to CityId
@Html.DropDownList("CityId", new SelectList(string.Empty, "Value", "Text"), htmlAttributes: new { id = "city", @class = "form-control" })
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消