亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有 ajax 生成選項的下拉列表未保存 CityId 屬性

帶有 ajax 生成選項的下拉列表未保存 CityId 屬性

慕容森 2023-12-19 20:51:04
我已經嘗試了三天,但我無法找出錯誤,因為代碼是正確的,但它的實現不是我想要的。 我正在使用三個表“州”、“城市”和“捐贈者”,我希望捐贈者在選擇州時選擇其州和城市,它顯示指定的城市,但在保存其信息時,城市 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" })



查看完整回答
反對 回復 2023-12-19
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號