我正在尝试使用新的视图模型数据更新我的视图,但是每次加载视图模型时,它都会在视图模型中显示正确的数据,即使在 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> </td><td>@Html.TextBoxFor(model => model.Address2)</td>
<td>@Html.Label("Position")</td><td>@Html.TextBoxFor(model => model.Position)</td>
</tr>
<tr>
<td> </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 @Html.CheckBoxFor(model => model.ContactByEmail) Email @Html.CheckBoxFor(model => model.ContactByLetter) Letter @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" /> <input type="submit" name="btnEdit" value="Edit" /> <input type="button" name="btnNew" value="New" disabled="disabled" /> <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');
}
});
});