我有一个表单提交,我试图通过一个隐藏字段将选定的下拉值发送到服务器端,但是它不起作用:

 @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @id = "registerFormId" }))
{
    <select class="form-control" id="districtId"></select>
    @Html.HiddenFor(m => m.DistrictId)
}


服务器端

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Register(RegisterViewModel registerViewModel)
    {
         // Hidden field value not bind here.
    }


注册视图模型:

public class RegisterViewModel
{
    // Some Properties there

    public int UserId { get; set; }
    public bool Status { get; set; }
    public int CountryId { get; set; }
    public bool IsGuest { get; set; }
    public int DistrictId { get; set; }
    public string ZipPostalCode { get; set; }
}

最佳答案

将选择的ID更改为另一个名称,而不要使用相同的名称。

<select class="form-control" id="another"></select>


在提交之前,必须确保已选择“隐藏”字段的值。

$('#another').on('change', function(){
   $('#DistrictId').val($(this).val());
});


祝好运!

08-27 22:18
查看更多