嗨,我在我看來有一個叫做聯繫人的領域。如果我點擊該按鈕,部分視圖將顯示爲彈出窗口,我在該字段附近保留了一個添加按鈕。部分視圖在mvc4中無法正常工作?
我PartialView
如果我輸入詳細信息,然後點擊保存按鈕說到POST操作來保存數據。現在我得到了2個問題。
1)CustomerName的值將爲空。
2)當我在輸入詳細信息後點擊彈出窗口中的創建按鈕後,它必須顯示相同的彈出窗口,或者必須顯示父視圖(Visitors Form)。在這個地方它重定向到部分視圖的主窗口。這在下圖中提到。我盡力解釋我的問題。請任何人幫我解決這個問題。
誤差運算
我查看代碼
@Html.Label("Contact Person", new { @class = "control-label" })
@Html.DropDownListFor(model => model.CustomerContactID, new SelectList(string.Empty, "Value", "Text"), "Please select a ContactPerson", new { @class = "form-control", type = "text", id = "CustomerContactID" })
<button id="AddContactPerson">Add ContactPerson</button>
<div id="AddNewContactPerson"></div>
我的J-查詢在父視圖中寫道
$(function() {
$('#AddNewContactPerson').dialog({
autoOpen: false,
width: 400,
height:500,
resizable: false,
title: 'Add New',
modal: true,
open: function(event, ui) {
$(this).load("@Url.Action("ContactPersonPartialView", "VisitorsForm")");
},
buttons: {
"Close": function() {
$(this).dialog("close");
}
}
});
$("#AddContactPerson").click(function() {
$("#AddNewContactPerson").dialog("open");
});
});
控制器代碼
public ActionResult ContactPersonPartialView()
{
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName");
return View("ContactPersonPartialView");
}
[HttpPost]
public JsonResult ContactPersonCreate(VisitorsViewModel VVviewmodel)
{
ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName", VVviewmodel.CustomerID);
var ContactIDObj = Guid.NewGuid();
var CustomerContactIDObj = Guid.NewGuid();
var CustomerContactObj = new CustomerContact()
{
CustomerContactID = CustomerContactIDObj,
CustomerID = VVviewmodel.CustomerID,
ContactReference = VVviewmodel.ContactPerson,
ContactID = ContactIDObj,
IsActive = true,
IsDeleted = false,
CreatedDate = DateTime.Now,
EditedDate = DateTime.Now,
LastActiveOn = DateTime.Now,
RowID = Guid.NewGuid(),
CreatedSessionID = Guid.NewGuid(),
EditedSessionID = Guid.NewGuid(),
OfflineMode = false,
OfflineID = Guid.NewGuid()
};
var ContactObj = new Contact()
{
ContactID = ContactIDObj,
DisplayName = VVviewmodel.CustomerID.ToString(),
PrintName = VVviewmodel.CustomerID.ToString(),
Phone1 = VVviewmodel.PhoneNo,
Mobile1 = VVviewmodel.MobileNo,
Email1 = VVviewmodel.Email,
Email2 = VVviewmodel.AlternateEmail
};
db.Contacts.Add(ContactObj);
db.CustomerContacts.Add(CustomerContactObj);
db.SaveChanges();
ModelState.Clear();
//return Json(new
//{
// redirectUrl = Url.Action("create", "VisitorsForm"),
// isRedirect = true
//});
return Json("VisitorsForm", JsonRequestBehavior.AllowGet);
}
我唐諾如何重定向到相同的彈出窗口局部視圖或父視圖請任何一個改正我的重定向控制器代碼。
我偏查看代碼
<div id="AddNewContactPerson">
@Html.Label("Customer Name")
@Html.DropDownList("CustomerID", "Select")
@Html.LabelFor(model => model.ContactPerson)
@Html.EditorFor(model => model.ContactPerson)
@Html.ValidationMessageFor(model => model.ContactPerson)
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
@Html.LabelFor(model => model.AlternateEmail)
@Html.EditorFor(model => model.AlternateEmail)
@Html.ValidationMessageFor(model => model.AlternateEmail)
@Html.LabelFor(model => model.PhoneNo)
@Html.EditorFor(model => model.PhoneNo)
@Html.ValidationMessageFor(model => model.PhoneNo)
@Html.LabelFor(model => model.MobileNo)
@Html.EditorFor(model => model.MobileNo)
@Html.ValidationMessageFor(model => model.MobileNo)
<p>
<input type="submit" value="Create" onclick="SaveContact()" />
</p>
</div>
</form>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.4-ui.min.js"></script>
<link href="~/Content/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
<script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>
<script type="text/javascript">
function SaveContact() {
var CustomerName = $("#CustomerID").val();
var ContactPerson = $("#ContactPerson").val();
var Email = $("#Email").val();
var AlternateEmail = $("#AlternateEmail").val();
var PhoneNo = $("#PhoneNo").val();
var MobileNo = $("#MobileNo").val();
var CustomerContact = {
"ContactPerson": ContactPerson, "Email": Email,
"AlternateEmail": AlternateEmail, "PhoneNo": PhoneNo,
"MobileNo": MobileNo, "CustomerID": CustomerName
};
$.post("/VisitorsForm/ContactPersonCreate", CustomerContact,
function (data) { if (data == 0) { location = location.href; } }, 'json');
}
</script>
我貼我的整個代碼在這裏,請任何一個檢查我的代碼。在部分視圖中,當我點擊創建按鈕並且沒有正確重定向時,客戶名稱將爲空。我盡力解釋我的問題。請任何一個解決這個問題。
提前謝謝..
首先刪除type =「submit」。你沒有提交表格。您在點擊按鈕時調用savecontact函數。 –