Sneha Dhandapani

Sneha Dhandapani

  • NA
  • 383
  • 35.5k

How to redirect from Child view to parent view?

Feb 26 2016 1:38 AM
Hi I have one field called contactperson  and i kept one add button near to that field.if i click that add button partial view will appear as popup window
 
My Parent view and Child View
 
 
 
if i click the add button near to contact person it will open the add new area popup window now i enter the details which is in the popup window it saving the data in db but it not redirect to parent view correctly. That is when i click the Create button which is popup window it shows this page but it saving the data in DB.
 
 
 
And also i created the properties of partial view in same view model which is used for parent view.
 My View
<div class="col-sm-4">
<div class="form-group">
@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 class="AddContactPerson">Add ContactPerson</button>
<div id="AddNewContactPerson"></div>
</div></div>
 
J query
 This code is to open the partial view as popup window
$('.AddContactPerson').on('click', function () {
$("#AddNewContactPerson").dialog({
autoOpen: true,
position: { my: "center", at: "top+350", of: window },
width: 1000,
resizable: false,
title: 'Add New Area',
modal: true,
open: function () {
$(this).load('@Url.Action("ContactPersonPartialView", "VisitorsForm")');
},
buttons: {
Cancel: function () {
$(this).dialog("close");
}
}
});
return false;
}); 
 
My partial View
<div class="editor-label">
@Html.LabelFor(model => model.CustomerName)
</div>
<div class="editor-field">
@Html.DropDownList("CustomerID", "Select")
</div>

<div class="editor-label">
@Html.LabelFor(model => model.ContactPerson)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ContactPerson)
@Html.ValidationMessageFor(model => model.ContactPerson)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div> 
 
Partialview jquery code
<script src="~/Scripts/jquery-ui.min-1.11.0.js"></script>
<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>
 
My Viewmodel
 
public Nullable<System.Guid> CustomerID { get; set; }
public string CustomerName { get; set; }
public Nullable<System.Guid> CustomerContactID { get; set; }
public Nullable<System.Guid> ContactID { get; set; }
public string ContactPerson { get; set; }
public string PhoneNo { get; set; }
public string MobileNo { get; set; }
public string Email { get; set; }
public string AlternateEmail { get; set; }
 
My Controller
public PartialViewResult ContactPersonPartialView() //Insert PartialView
{

ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "DisplayName");
return PartialView ();
}

[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);

}
 
All are working fine it open the partial view as popup window when i click the button and if i enter the details it save the value in Db when i click create button in partial view but it not redirect the page correctly. please any one correct my http post return json code . i tried many ways but its not working.  please give me suggession.
 
Advance Thanks
 
 
 
 

Answers (1)