2011-10-14 23 views
2

如果用戶單擊使用上面的地址,則值在Address1中設置並被禁用。 該值未在表單提交中傳遞,但我可以從Controller中的model.Address中獲取該值。如果ModelState.IsValid無效,我將字段model.Address1返回空,我如何使用model.Address填充它?Jquery只讀文本框在MVC控制器上發佈後不可見

View 
$("#MailAddressAsAbove").click(function() { 
    if ($(this).attr("checked") == true) { 
     $('#Address1').attr("disabled", true); 
    } 
} 

控制器

[HttpPost] 
public ActionResult Enquiry(PersonViewModel model) 
{ 
    if (model.MailAddressAsAbove) 
    { 
     model.Address1 = model.Address; 
    } 

    if (!ModelState.IsValid) 
    { 
     return Enquiry(); 
    } 

}

感謝

回答

2

隨時糾正我,如果我不能得到你想要問什麼。

首先,所有禁用的表單元素將不會包含在表單提交中。 但只讀元素將被包括在內。

所以可能你可以嘗試使用readonly而不是disabed屬性?

$("#MailAddressAsAbove").click(function() { 
    if ($(this).attr("checked") == true) { 
     $('#Address1').attr("readonly", "readonly"); 
    } 
} 
+0

這工作感謝您的幫助昆西。 –