MVC3 - View未使用Viewmodel上的新数据进行更新

时间:2013-04-09 14:54:23

标签: asp.net-mvc-3

我正在尝试使用新的viewmodel数据更新我的视图,但每次加载viewmodel时,它都会在viewmodel中显示正确的数据,即使在jquery成功调用中的数据中也是如此,但它不会更新视图。

我正在粘贴下面的代码,请检查并告诉我,我做错了什么?

两天之后,我很生气。

视图模型

namespace CRMTest.Models.ViewModels
{
public class ContactViewModel
{
    private Xrm.Contact item;

    public string ContactName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }
    public string CompReg { get; set; }
    public string OurRef { get; set; }
    public string RefType { get; set; }
    public string Contact { get; set; }
    public string Salutation { get; set; }
    public string Position { get; set; }
    public string Telephone { get; set; }
    public string Fax { get; set; }
    public string Mobile { get; set; }
    public string ClientServices { get; set; }
    public bool ContactByTel { get; set; }
    public bool ContactByEmail { get; set; }
    public bool ContactByLetter { get; set; }
    public bool ContactByFax { get; set; }
    public string Email { get; set; }
    public string Website { get; set; }
    public string Notes { get; set; }
    public DateTime UpdatedDate { get; set; }
    public string UpdatedBy { get; set; }

    public ContactViewModel(Xrm.Contact item)
    {
        ContactName = item.Address1_Name;
        Address1 = item.Address1_Line1;
        Address2 = item.Address1_Line2;
        Address3 = item.Address1_Line3;
        Town = item.Address1_City;
        County = item.Address1_County;
        Postcode = item.Address1_PostalCode;
        OurRef = item.NickName;
        Contact = item.Salutation+" "+item.FullName;
        Salutation = item.Salutation+" "+item.LastName;
        Position = item.JobTitle;
        Telephone = item.Address1_Telephone1;
        Fax = item.Address1_Fax;
        Mobile = item.Address1_Telephone2;
        Email = item.EMailAddress1;
        Website = item.WebSiteUrl;
    }        
}

控制器

 public ActionResult Index()
    {
        //CRM Start
        Session.Add("ServiceProxy", serviceProxy);

        //CRM End

        var service = Session["ServiceProxy"] as Microsoft.Xrm.Sdk.IOrganizationService;
        service = (IOrganizationService)serviceProxy;


        if (service != null)
        {              
            using (var context2 = new XrmServiceContext(service))
            {
                var contacts = context2.ContactSet.FirstOrDefault(); 
                    ModelState.Clear();
                    var contactsViewModel = new ContactViewModel(contacts);

                return View(contactsViewModel);
            }
        }
        else
        {
            ModelState.AddModelError("", "Service proxy is missing is session state. Did you log in?");
            return RedirectToAction("LogOn", "Account");
        }
    }

    [HttpPost]
    public ActionResult Index(string strSearchParam)
    {
        if (!ModelState.IsValid)
        {
            if (Request.IsAjaxRequest())
                return PartialView("FindCustomer", strSearchParam);

            return View(strSearchParam);
        }

        if (service != null)
        {
            ModelState.Clear();
            using (var context2 = new XrmServiceContext(service))
            {
                var contacts = context2.ContactSet.Where(c => c.NickName.Equals(strSearchParam)).FirstOrDefault();

                if (contacts != null)
                {                       
                    var contactsViewModel = new ContactViewModel(contacts);
                    ModelState.Clear();

                    return View("Index",contactsViewModel);
                }
                else
                    return View();
            }
        }
        return View();
    }

查看

@model CRMTest.Models.ViewModels.ContactViewModel

@{
ViewBag.Title = "Index";
 }

<script src="@Url.Content("~/Content/Site.css")" type="text/javascript"></script>    
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css"    type="text/css" media="all" /> 
 <script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript">  </script>
  <script src="@Url.Content("~/Scripts/jquery-ui-1.8.20.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

 @using (Html.BeginForm("frmContactForm"))
 {
  @Html.ValidationSummary(true); 
<div id="mainForm">
    <table border="1">
        <tr>
            <td>@Html.LabelFor(x => x.ContactName)</td><td>@Html.TextBoxFor(model => model.ContactName)</td>
            <td>@Html.Label("Our Ref/Type")</td><td>@Html.DisplayFor(x => x.OurRef)</td><td><input id="Contact" name="Contact" type="text" value="Mrs Lakhbir"></td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Address1)</td><td>@Html.TextBoxFor(model => Model.Address1)</td>
            <td>@Html.Label("Contact/Salu'n")</td><td>@Html.TextBoxFor(model => model.Contact)</td><td>@Html.TextBoxFor(model => model.Salutation)</td>
        </tr>
        <tr>
            <td>&nbsp;</td><td>@Html.TextBoxFor(model => model.Address2)</td>
            <td>@Html.Label("Position")</td><td>@Html.TextBoxFor(model => model.Position)</td>
        </tr>
        <tr>
            <td>&nbsp;</td><td>@Html.TextBoxFor(model => model.Address3)</td>
            <td>@Html.Label("Tel/Fax")</td><td>@Html.TextBoxFor(model => model.Telephone)</td><td>@Html.TextBoxFor(model => model.Fax)</td>
        </tr>
        <tr>
            <td>@Html.Label("Town/City")</td><td>@Html.TextBoxFor(model => model.Town)</td>
            <td>@Html.Label("Mobile")</td><td>@Html.TextBoxFor(model => model.Mobile)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.County)</td><td>@Html.TextBoxFor(model => model.County)</td>
            <td>@Html.Label("Client Services")</td><td>@Html.TextBoxFor(model => model.ClientServices)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Postcode)</td><td>@Html.TextBoxFor(model => model.Postcode)</td>
            <td>@Html.Label("Contact by")</td><td colspan="2">@Html.CheckBoxFor(model => model.ContactByTel) Tel&nbsp;@Html.CheckBoxFor(model => model.ContactByEmail) Email&nbsp;@Html.CheckBoxFor(model => model.ContactByLetter) Letter&nbsp;@Html.CheckBoxFor(model => model.ContactByFax) Fax</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.CompReg)</td><td>@Html.TextBoxFor(model => model.CompReg)</td>
            <td>@Html.Label("Email")</td><td>@Html.TextBoxFor(model => model.Email)</td>
        </tr>
        <tr>
            <td>@Html.Label("1 of 5906")</td><td><input type="button" id="btnFind" value="Find" />&nbsp;<input type="submit" name="btnEdit" value="Edit" />&nbsp;<input type="button" name="btnNew" value="New" disabled="disabled" />&nbsp;<input type="button" name="btnAccount" value="Account" disabled="disabled" /></td>
            <td>@Html.Label("Website")</td><td>@Html.TextBoxFor(model => model.Website)</td>
        </tr>
    </table>
</div>

}

<div id="findCustomer">
    <div class="popup_content radius" >
        <table border="1">
            <tr>
                <td>@Html.Label("OurRef")</td>
                <td>@Html.TextBox("txtOurRef")<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.ContactName)</td>
                <td>@Html.TextBoxFor(model => Model.ContactName, new { @class = "form_input radius" })<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.CompReg)</td>
                <td>@Html.TextBoxFor(model => Model.CompReg, new { @class = "form_input radius" })<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.Address1)</td>
                <td>@Html.TextBoxFor(model => Model.Address1, new { @class = "form_input radius" })<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.Address2)</td>
                <td>@Html.TextBoxFor(model => Model.Address2, new { @class = "form_input radius" })<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.Telephone)</td>
                <td>@Html.TextBoxFor(model => Model.Telephone, new { @class = "form_input radius" })<br /></td>
            </tr>
            <tr>
                <td>@Html.LabelFor(model => Model.Postcode)</td>
                <td>@Html.TextBoxFor(model => Model.Postcode, new { @class = "form_input radius" })<br /></td>
            </tr>
            @*  <tr>
                <td><input type="button" id="btnSearch" value="Search" class="submit submit_green fltl"></td>
                <td><input type="button" id="btnClose" value="Cancel" class="submit submit_green fltl"></td>
            </tr>*@
        </table>
    </div>
</div> 

    <script type="text/javascript">

        $.ajaxSetup({ cache: false });

        $(document).ready(function () {

            $("#btnFind")
                .click(function () {
                    $("#findCustomer").dialog("open");
                });

            $("#findCustomer")
                .dialog({
                    autoOpen: false,
                    height: 330,
                    width: 350,
                    modal: true,
                    title: 'Find Customer',
                    resizable: false,
                    buttons: {
                        'Search': function () {
                            var id = $("#txtOurRef").val();
                            $("#mainForm").load(id);
                            var form = $('#frmContactForm');
                            $.ajax({
                                type: 'POST',
                                url: '/Contacts/Index',
                                data: { strSearchParam: id },
                                cache: false,
                                //dataType: 'json',
                                success: function (data) {
                                    alert($("#mainForm"));
                                    alert(data);
                                    //form.replaceWith(data);
                                    $("#mainForm").load(data);
                                    $("#findCustomer").dialog("close");
                                },
                                error: function (jqXHR, textStatus, errorThrown) { debugger;/*see what happened */ }
                            })
                        },
                        'Cancel': function () {
                            $("#findCustomer").dialog("close");
                        }
                    },
                    error: function (error) {
                        alert('Fail');
                    }   
                });
        });

1 个答案:

答案 0 :(得分:1)

您使用

 $("#mainForm").load(data);

但如果数据不是网址,则应使用

 $("#mainForm").html(data);

可能你只想替换mainform的内容,所以它会是这样的:

 $("#mainForm").html($(data).find("#mainForm").html());
相关问题