это мой код ajax / jquery, который отправляет значение поля телефона на контроллер
<script type="text/javascript">
debugger;
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#button").click(function () {
debugger;
alert("phonechanged");
$.ajax({
type: "POST",
dataType: 'json',
contentType: 'application/json',
url: '@Url.Action("CheckPhone")',
//data: { phone: $("#phone").val() },
data: JSON.stringify({ phone: $("#phone").val() }),
success: function (data) {
debugger;
var obj = JSON.parse(data);
$("#firstName").val(obj.firstName);
},
error: function (ex) {
debugger;
alert('Failed to retrieve customer.' + ex);
}
});
return false;
})
});
</script>
и это мой метод контроллера, который должен вернуть клиента, если таковой имеется, и затем ajax изменить значения поля. мой код не работает Я также не знаю, как проверить нулевые значения, если ни у одного клиента нет этого номера телефона.
[HttpPost]
public JsonResult CheckPhone(string phone)
{
Customer cm = new Customer();
bool check = false;
Leads_Management_SystemEntities lmsEntities = new Leads_Management_SystemEntities();
cm = lmsEntities.Customers.FirstOrDefault(x => x.phoneNum1 == phone || x.phoneNum2 == phone || x.phoneNum3 == phone);
string value = string.Empty;
value = JsonConvert.SerializeObject(cm, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
return Json(value);
}
код бритвы
<div class="form-group">
@Html.LabelFor(model => model.firstName, new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.firstName, new { id = "firstName" })
@Html.ValidationMessageFor(model => model.firstName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email)
@Html.ValidationMessageFor(model => model.email)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.city, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.city)
@Html.ValidationMessageFor(model => model.city)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.country, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.country)
@Html.ValidationMessageFor(model => model.country)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.phoneNum1, new { @class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.phoneNum1, new { id = "phone" })
@Html.ValidationMessageFor(model => model.phoneNum1)
</div>
</div>
<input type="submit" value="submit" id="button" />
<div class="form-group">
@Html.LabelFor(model => model.phoneNum2, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.phoneNum2)
@Html.ValidationMessageFor(model => model.phoneNum2)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.phoneNum3, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.phoneNum3)
@Html.ValidationMessageFor(model => model.phoneNum3)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.propertyOwned, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.propertyOwned)
@Html.ValidationMessageFor(model => model.propertyOwned)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.address, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.address)
@Html.ValidationMessageFor(model => model.address)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CNIC, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CNIC)
@Html.ValidationMessageFor(model => model.CNIC)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lastName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.lastName)
@Html.ValidationMessageFor(model => model.lastName)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.budgetTo, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.budgetTo)
@Html.ValidationMessageFor(model => model.budgetTo)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.budgetFrom, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.budgetFrom)
@Html.ValidationMessageFor(model => model.budgetFrom)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.leadScoreId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.leadScoreId, (IEnumerable<SelectListItem>)ViewBag.lsList, "Select leadscore")
@*@Html.EditorFor(model => model.leadScoreId)*@
@Html.ValidationMessageFor(model => model.leadScoreId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.plotSize, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.plotSize)
@Html.ValidationMessageFor(model => model.plotSize)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.statusId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.statusId, (IEnumerable<SelectListItem>)ViewBag.statusList, "Select status")
@*@Html.EditorFor(model => model.statusId)*@
@Html.ValidationMessageFor(model => model.statusId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.uniqueHandle, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.uniqueHandle)
@Html.ValidationMessageFor(model => model.uniqueHandle)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.plotTypeId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.plotTypeId, (IEnumerable<SelectListItem>)ViewBag.ptList, "Select plotType")
@*@Html.EditorFor(model => model.plotTypeId)*@
@Html.ValidationMessageFor(model => model.plotTypeId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Block, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Block)
@Html.ValidationMessageFor(model => model.Block)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.commentsDetails, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.commentsDetails)
@Html.ValidationMessageFor(model => model.commentsDetails)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.typeId, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.typeId, (IEnumerable<SelectListItem>)ViewBag.stList, "Select sourcetype")
@*@Html.EditorFor(model => model.typeId)*@
@Html.ValidationMessageFor(model => model.typeId)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.sourceTime, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.sourceTime)
@Html.ValidationMessageFor(model => model.sourceTime)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.commentsSource, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.commentsSource)
@Html.ValidationMessageFor(model => model.commentsSource)
</div>
</div>
@*this form group wasnot auto generated*@
<div class="form-group">
@Html.LabelFor(model => model.tags, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(model => model.tags, (MultiSelectList)(ViewData["tags"]) as SelectList)
@*@Html.EditorFor(model => model.tags)*@
@Html.ValidationMessageFor(model => model.tags)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.features, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(model => model.features, (MultiSelectList)(ViewData["features"]) as SelectList)
@*@Html.EditorFor(model => model.features)*@
@Html.ValidationMessageFor(model => model.features)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}