簡體   English   中英

部分視圖模型的值在提交時為null

[英]Values for partial view model are null on submit

我無法成功地將部分視圖中的值發布到我的操作中 - 所有屬性都為null。

部分視圖模型:

public class AddressViewModel
{
   public string ClientNumber { get; set; }
   public string Line1 { get; set; }
   public string Line2 { get; set; }
   public string Line3 { get; set; }
   public string Suburb { get; set; }
   public string PostalCode { get; set; }
}

局部視圖:

@model Models.AddressViewModel
@{
    Layout = null;
}

@using (Html.BeginForm("UseAddress", "Home"))
    { 
<div>
    <table>
        <tr>
            <td>
                <div class="display-label">
                    @Html.DisplayNameFor(model => model.Line1)
                </div>
            </td>
            <td>
                <div class="display-field">
                    @Html.DisplayFor(model => model.Line1)
                </div>
            </td> 
........
        </tr>
    </table>

    <input type="submit" name="UseAddress" id="submitbutton" value="Use Address" />

</div>
}

行動:

[HttpPost]
[Authorize]
public ActionResult UseAddress(AddressViewModel model)
{
    return RedirectToAction("Index", "Home");
}

通過選擇下拉列表在頁面上呈現部分視圖,如下所示:

<script type="text/javascript">
    $(function () {
        $('#AddressTypeDropdownList').change(function () {          
            var url = $(this).data('url');          
            $('#Address').load(url);
        });
    });
</script>

    @Html.DropDownListFor(
    x => x.SelectedAddressTypeId,
    new SelectList(Model.AddressTypes, "Value", "Text"),
    "-- Select Address Type --",
         new
         {
             id = "AddressTypeDropdownList",
             data_url = Url.Action("_Address", "Home")
         }
    )
    <div id="Address"></div>          


    public ActionResult _Address()
    {        
        AddressViewModel addressViewModel = new AddressViewModel {
            ClientNumber = "test"          
        };         

        return PartialView(addressViewModel);
    }

我希望UseAddress方法在我單擊提交按鈕時使用model.ClientNumber == "test"但它是null ...有什么明顯的我做錯了嗎?

DisplayFor不為字段創建輸入,因此不會發布。 你需要添加

@Html.HiddenFor(model => model.Line1)
....

發布值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM