我創建了一個名爲AddCust.cshtml的彈出窗口。一旦用戶點擊了btnSaveConsumer,它應該提取所有輸入的值。但我不知道如何通過控制器。我試圖使用像下面的JavaScript,但它不傳遞任何東西從AddCust.cshtml。請指教,謝謝我如何將窗口彈出的數據傳遞給控制器
AddCust.cshtml
@model HHIMS_Web_App.Models.ConsumerModel
@{
}
<br />
<div>
<fieldset id="AddNewConsumer">
<br /> <br />
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.HRN)
@Html.EditorFor(model => model.HRN)
</div>
<div class="smallBox">
@Html.LabelFor(model => model.DOB)
@Html.EditorFor(model => model.DOB)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.GivenName1)
@Html.EditorFor(model => model.GivenName1)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.FamilyName1)
@Html.EditorFor(model => model.FamilyName1)
</div>
<div class="smallBox">
@Html.LabelFor(model => model.Ethnicity)
@Html.EditorFor(model => model.Ethnicity)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.GivenName2)
@Html.EditorFor(model => model.GivenName2)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.FamilyName2)
@Html.EditorFor(model => model.FamilyName2)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.Address)
@Html.EditorFor(model => model.Address)
</div>
<div class="smallBox">
@Html.LabelFor(model => model.CarerContactName)
@Html.EditorFor(model => model.CarerContactName)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.Community)
@Html.EditorFor(model => model.Community)
</div>
<div class="smallBox">
@Html.LabelFor(model => model.CarerContact)
@Html.EditorFor(model => model.CarerContact)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.State)
@Html.EditorFor(model => model.State)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.PostCode)
@Html.EditorFor(model => model.PostCode)
</div>
</div>
<div>
<div class="addConsumerInfo">
@Html.LabelFor(model => model.Phone)
@Html.EditorFor(model => model.Phone)
</div>
<div class="smallAddAndCancel">
<input type="button" id="btnCancel" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Cancel" value="Cancel" onclick="window.close()" />
<input type="button" id="btnSaveConsumer" style="height:33px; width:70px; font-size:14px; background-color:#3399FF" class="k-button" title="Save" value="Save" />
</div>
</div>
</fieldset>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#btnSaveConsumer').click(function() {
var hrn;
var community;
var ethnicity;
var familyName;
var givenName;
if (hrn) {
$filter.push({ field: "HRN", operator: "contains", value: hrn });
}
if (community) {
$filter.push({ field: "Community", operator: "contains", value: community });
}
if (familyName) {
$filter.push({ field: "FamilyName", operator: "contains", value: familyName });
}
if (givenName) {
$filter.push({ field: "GivenName", operator: "contains", value: givenName });
}
if (ethnicity) {
$filter.push({ field: "Ethnicity", operator: "contains", value: ethnicity });
}
$.ajax({
type: 'POST',
url: "@(Url.Content("~/ConsumerDetails/CreateConsumerList/"))",
data: {
"HRN": hrn,
"Community":community,
"FamilyName1":familyName,
"GivenName1":givenName,
"Ethnicity":ethnicity,
},
});
});
});
</script>
控制器:
[HttpPost]
public ActionResult CreateConsumerList(ConsumerModel model)
{
if (ModelState.IsValid)
{
HHIMS_DataAccessLayer.Consumers dalModel = new HH.Consumers();
Mapper.CreateMap<ConsumerModel, HH.Consumers>();
Mapper.Map(model, dalModel);
dbConsumer.SaveConsumer(dalModel);
}
return RedirectToAction("Index");
}
「你並不需要的JavaScript」,但你正在使用AJAX;) – 2013-05-02 03:16:44
我的意思是$就調用。這是一個額外的步驟。爲什麼他不使用MVC framkework完全用System.Web.Mvc.Ajax – HaBo 2013-05-02 03:20:11
感謝您編輯您的答案,這是一個更好的內容。現在關於「所有不必要的」,這就是所謂的「偏好」。使用'AjaxHelper'並不是最好的方法,特別是在一個大型項目中,你擁有優秀的jQuery開發人員和CSS專家的UI人員。而要求是你必須有乾淨的服務器端代碼的用戶界面。你有沒有遇到過一個項目,它會告訴你使用表單標籤而不是使用'using'?我知道,對吧。所以底線,這是一個偏好,「不必要」不是最好的術語;) – 2013-05-02 03:29:47