简体   繁体   English

部分视图在mvc4中无法正常工作?

[英]Partial view is not working correctly in mvc4?

Hi i have one field called contact person in my view. 嗨,我认为一个字段称为联系人。 i kept one add button near to that field if i click that button the partial view will appear as pop-up window. 如果单击该按钮,则在该字段附近保留一个添加按钮,部分视图将显示为弹出窗口。

My PartialView 我的PartialView

在此处输入图片说明 If i enter the details and click the save button it comes to the post action to save the data. 如果我输入详细信息并单击“保存”按钮,则该操作将用于发布数据以保存数据。 Now i got 2 issues in that. 现在我有2个问题。

1) The Value of CustomerName will be null. 1)CustomerName的值将为null。

2) After i click create button in popup window after enter the details either it have to show the same popup window or it have to show the parent view(Visitors Form) i got issue in this step. 2)输入详细信息后,在弹出窗口中单击创建按钮后,它必须显示相同的弹出窗口,或者必须显示父视图(访问者表单),这是我在此步骤中遇到的问题。 In this place it redirect to main window of partial view. 在这个地方,它重定向到局部视图的主窗口。 which is mentioned in below image. 下图提到了这一点。 i tried my level best to explain my issue . 我尽力解释我的问题。 please any one help me to resolve this issue. 请任何人帮助我解决此问题。

Error Op 错误操作

Error Output 错误输出

My View Code 我的查看代码

 @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>

My j-query wrote in parent view 我的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");
    });
  });

Controller Code 控制器代码

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

I donno how to redirect to same pop-up window partial view or to parent view please any one correct my redirect controller code. 我不知道如何重定向到同一弹出窗口的部分视图或父视图,请任何一个更正我的重定向控制器代码。

My Partial View Code 我的部分查看代码

  <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>

I posted my whole code here please any one check my code. 我在这里张贴了我的整个代码,请任何人检查我的代码。 In partial view the customer name will be null when i click create button and it didn't redirect correctly. 在局部视图中,当我单击“创建”按钮时,客户名称将为null,并且未正确重定向。 i try my level best to explain my issue. 我尽我所能解释我的问题。 please any one give solution for this problem. 请任何人提供解决此问题的方法。

Advance Thanks.. 提前谢谢..

For the 1 part of your question: 对于问题的第一部分:

CustomerName is probably null, since you have no input with that name on your partial. CustomerName可能为null,因为您的部分中没有使用该名称的输入。 You only have @Html.DropDownList("CustomerID", "Select") which will pass in your CustomerID only, not the CustomerName . 您只有@Html.DropDownList("CustomerID", "Select") ,它们只会传入您的CustomerID ,而不会传入CustomerName But I don't think that is a huge problem, I suppose you'll store the ID in the database anyway. 但是我认为这不是一个大问题,我想您还是将ID存储在数据库中。

If you need the name, you can probably simply get it from the database based on the CustomerID value. 如果需要该名称,则可以简单地根据CustomerID值从数据库中获取它。

The second part is not completely clear for me, what should be the condition which makes a different between the two windows to show? 第二部分对我来说还不是很清楚,在显示的两个窗口之间有什么区别的条件应该是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM