[英]How I do bind data to DisplayFor from event blur on asp.net mvc
I'm using event blur for lost focus on textbox. 我正在使用事件模糊功能来失去对文本框的关注。 But I only bind data to textboxfor cannot bind data to labelfor or displayfor.
但是我仅将数据绑定到textboxfor,而不能将数据绑定到labelfor或displayfor。 How can I do ?
我能怎么做 ?
My java script: 我的Java脚本:
$('#PGId').blur(function () {
var errormsg = "";
var id = $('#PGId').val();
$.ajax({
type: "GET",
url: '@Url.Action("GetDetailPG", "TimeSheetHeader")',
data: { pgId: id },
dataType: "json",
success: function (data) {
$("#FirstName").val(data.FisrtName)
$("#LastName").val(data.LastName)
$("#Shiff").val(data.ShiffId)
},
error: function (jqXHR, exception) {
$('#error').html("Primitive Functions not allowed.")
}
});
})
My razor view: 我的剃刀视图:
<div class="col-md-3 form-group">
@Html.LabelFor(model => model.PGId) <em>( * )</em>
@Html.TextBoxFor(model => model.PGId, new { @class = "form-control input-sm", @name = "normal" })
@Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)
</div>
<div class="col-md-3 form-group">
@Html.LabelFor(model => model.Shiff)
<div>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.Shiff, "M") Morning
</label>
<label class="radio-inline">
@Html.RadioButtonFor(model => model.Shiff, "N") Night
</label>
</div>
<span style="color:red" class="help-block">@Html.ValidationMessageFor(model => model.Shiff)</span>
</div>
@Html.DisplayFor()
does not generate an element with an id
attribute, nor a control with a value
attribute so you can't use $("#FirstName").val(data.FirstName)
. @Html.DisplayFor()
不会生成具有id
属性的元素,也不会生成具有value
属性的控件,因此您不能使用$("#FirstName").val(data.FirstName)
。 Instead create a container element with the required id
and update its contents in the ajax call (not sure what ShiffId
is - you didn't show any html for it) 而是创建一个具有所需
id
的容器元素,并在ajax调用中更新其内容(不确定ShiffId
是什么-您没有为此显示任何html)
@Html.TextBoxFor(m => m.PGId, new { @class = "form-control input-sm" })
<div id="firstname">@Html.DisplayFor(m => m.FirstName)</div>
<div id="lastname">@Html.DisplayFor(m => m.LastName)</div>
then in the script 然后在脚本中
success: function (data) {
$("#firstname").text(data.FirstName)
$("#lastname").text(data.LastName)
},
Note also you should be using the change
event of the textbox (not blur
) to avoid unnecessary calls to the server when the user is just tabbing through controls. 还要注意,您应该使用文本框的
change
事件(而不是blur
),以避免当用户只是在控件之间切换时不必要地调用服务器。
Side note: Why are you trying to override the name
attribute of the textbox for property PGId
? 旁注:为什么要覆盖属性
PGId
的文本框的name
属性?
Edit 编辑
Based on OP edit showing html for ShiffId
, to update the radio button, add a class name to the radio buttons (note: class
attribute added for easier selection and id
attribute added so no duplicates) 基于OP编辑显示
ShiffId
html,以更新单选按钮,向单选按钮添加类名称(注意:添加了class
属性以方便选择,并添加了id
属性,因此没有重复项)
@Html.RadioButtonFor(model => model.Shiff, "M", new { id = "shiff-m", @class = "shiff" })
@Html.RadioButtonFor(model => model.Shiff, "N", new { id = "shiff-n", @class = "shiff" })
and replace $("#Shiff").val(data.ShiffId)
with (assumes data.Shiff
will be either "M" or "N") 并将
$("#Shiff").val(data.ShiffId)
替换为(假定data.Shiff
为“ M”或“ N”)
$('.shiff[value="' + data.Shiff + '"]').prop('checked', true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.