在asp.net MVC中,如何使用AutoPostBacking更改部分视图中TextBox的值 [英] In asp.net MVC, How to change the value of TextBox in a Partial View using AutoPostBacking
问题描述
我对MVC Asp.net中的自动回发非常陌生. 我需要更改textbox2的值,而自动回发部分视图和自动回发应仅在单击文本框1后发生
I am very new to Auto Postbacking in MVC Asp.net. i need to change the value of textbox2, While Auto postbacking the Partial View and Autopostback should happen only after a click in textbox1
问题:自动回发后,textbox2并未更改该值.
Issue: textbox2 is not changing the value after autopostbacking.
进一步的发现:Post Success Function正在工作("function SetData(data)"),因此当我调试代码时,它自动过帐到ActionMethod:"Test()"并执行代码,然后返回_PartialViewTwo并执行了代码dere ,但它没有更改TextBox2的值
Further findings: Post Success Function is working("function SetData(data)") , so when i debugged the code, it AutoPostBacked to ActionMethod:"Test()" and executed the code and returned to _PartialViewTwo and executed code dere , but it did not changed the TextBox2 value
注意:TextBox1位于PartialViewOne中,TextBox2位于PartialViewTwo
note: TextBox1 is in PartialViewOne ,TextBox2 is in PartialViewTwo
在此先感谢您的帮助.
//GET:主页
public ActionResult Index()
{
Response.Write("working Index");
Student Model = new Student();
Model.RollNo = 1;
Model.Name = "Arun";
return View(Model);
}
[HttpPost]
public PartialViewResult Test()
{
Student Model = new Student();
Response.Write("working test3");
Model.Name = "Afsal";
return PartialView("_PartialViewTwo",Model) ;
}
主视图
@model test1.Models.Student
@using (Html.BeginForm("Index","Home",FormMethod.Post))
{
<table>
<tr> <td>@Html.LabelFor(x=>x.RollNo)
<td>@Html.Partial("_PartialViewOne",Model)</td>
</tr>
<tr>
<td><div id="divpartialview">@Html.Partial("_PartialViewTwo",Model)</div></td>
</tr>
</table>
}
PartialViewOne
@Html.TextBox("txt1")
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(fun1)
function fun1()
{
alert("ready");
$("#txt1").click(fun2);
}
function fun2()
{
alert("click");
$.ajax({
url: '@Url.Action("Test/Home")',
data: null,
cache: false,
type: "POST",
dataType: "html",
success: function (data, textStatus, XMLHttpRequest) {
SetData(data);
}
});
}
function SetData(data) {
alert("again");
$("#divPartialView").html(data); // HTML DOM replace
}
</script>
PartialViewtwo
@model test1.Models.Student
@Model.Name
推荐答案
@To all:问题已解决.它在jquery函数"function SetData(data)"中的选择器" $("#divPartialView) .html(data)"中区分大小写,现在更改为" $("#divpartialview)" ........ ....:P
@To all : Issue is fixed. its a case sensitive error in the selector "$("#divPartialView").html(data)" inside the jquery function "function SetData(data)" , now it is changed to"$("#divpartialview ")" ........ ....:P
function SetData(data) {
alert("again");
$("#divpartialview ").html(data); // HTML DOM replace
}
这篇关于在asp.net MVC中,如何使用AutoPostBacking更改部分视图中TextBox的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!