如何在asp.net mvc中使用Ajax更新View?(How to update View using Ajax in asp.net mvc?)
我有一个简单的表格,下拉列表中有人名(参展商)。 在我选择其中一个并点击“获取参展商数据”链接后,我想仅更新我的主页网站的一部分并显示所选参展商的数据。 项目中我的文件夹结构: 我的家庭控制器如下所示:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebApplication5.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } public PartialViewResult GetExhibitorDataById(int? Id) { List<Exhibitor> exhibitors = new List<Exhibitor>() { new Exhibitor() { Id=1, Name= "Tom", Surname="Cruise" }, new Exhibitor() { Id=2, Name= "Jennifer", Surname="Lopez" }, }; if (Id == 1) { //return PartialView("_Exhibitor", exhibitors[0]); Session["ExhibitorData"] = exhibitors[0]; return PartialView("_Exhibitor"); } else if(Id==2) { //return PartialView("_Exhibitor", exhibitors[1]); Session["ExhibitorData"] = exhibitors[1]; return PartialView("_Exhibitor"); } else { //return PartialView("_Exhibitor", new Exhibitor()); Session["ExhibitorData"] = new Exhibitor(); return PartialView("_Exhibitor"); } } public class Exhibitor { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } } } }
Home文件夹中的我的索引视图代码如下所示:
@using WebApplication5.Controllers <h2>Exhibitors</h2> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> @Html.DropDownList("ExhibitorsList", new List<SelectListItem> { new SelectListItem {Text ="Tom Cruise", Value = "1" }, new SelectListItem {Text ="Jennifer Lopez", Value = "2" }, }, "Select Exhibitor" ) @Ajax.ActionLink("Get Exhibitor data", "GetExhibitorDataById", new { Id = 1 }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divExhibitors", // ID of the HTML element to update InsertionMode = InsertionMode.Replace // Replace the existing contents }) <div id="divExhibitors"> </div>
但我想将Ajax.ActionLink的参数Id设置为DropDownList中名为“ExhibitorsList”的值,我不知道该怎么做。 部分查看代码“_Exhibitor”如下所示:
@using WebApplication5.Controllers <table> @if (Session["ExhibitorData"] != null) { <tr> <td>Id</td> @*@{HomeController.Exhibitor exhibitor = ((HomeController.Exhibitor)(@Session["ExhibitorData"]))};*@ @*<td>@exhibitor.Id</td>*@ <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Id</td> </tr> <tr> <td>Name</td> <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Name</td> </tr> <tr> <td>surname</td> <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Surname</td> </tr> } </table>
当我尝试运行我的应用程序然后单击Aajax.ActionLink时出错了,因为在我单击Ajax.ActionLink之后我转到了不同的URL。 主页视图:
点击Ajax.ActionLink之后
我想要的是从下拉列表中选择人名,然后单击Ajax.ActionLink并获取选定人员(参展商)的数据,而无需重新定向或刷新网站,就像我在开始时所写的那样。 我也很好奇是否有可能只使用一个View-“Index”而不使用partialview。
I have a simple form with drop down list with people names(exhibitors). After I choose one of them and click link “Get Exhibitor data” I want to update only a part of my Home site and show data of chosen exhibitor. Structure of my folders in project: My home controller looks like follows:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebApplication5.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } public PartialViewResult GetExhibitorDataById(int? Id) { List<Exhibitor> exhibitors = new List<Exhibitor>() { new Exhibitor() { Id=1, Name= "Tom", Surname="Cruise" }, new Exhibitor() { Id=2, Name= "Jennifer", Surname="Lopez" }, }; if (Id == 1) { //return PartialView("_Exhibitor", exhibitors[0]); Session["ExhibitorData"] = exhibitors[0]; return PartialView("_Exhibitor"); } else if(Id==2) { //return PartialView("_Exhibitor", exhibitors[1]); Session["ExhibitorData"] = exhibitors[1]; return PartialView("_Exhibitor"); } else { //return PartialView("_Exhibitor", new Exhibitor()); Session["ExhibitorData"] = new Exhibitor(); return PartialView("_Exhibitor"); } } public class Exhibitor { public int Id { get; set; } public string Name { get; set; } public string Surname { get; set; } } } }
My Index View code from Home folder looks like this:
@using WebApplication5.Controllers <h2>Exhibitors</h2> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> @Html.DropDownList("ExhibitorsList", new List<SelectListItem> { new SelectListItem {Text ="Tom Cruise", Value = "1" }, new SelectListItem {Text ="Jennifer Lopez", Value = "2" }, }, "Select Exhibitor" ) @Ajax.ActionLink("Get Exhibitor data", "GetExhibitorDataById", new { Id = 1 }, new AjaxOptions() { HttpMethod = "GET", UpdateTargetId = "divExhibitors", // ID of the HTML element to update InsertionMode = InsertionMode.Replace // Replace the existing contents }) <div id="divExhibitors"> </div>
But I would like to set parameter Id of Ajax.ActionLink, to value from DropDownList called “ExhibitorsList” and I don’t know how to do that. Partial View code “_Exhibitor” looks like this:
@using WebApplication5.Controllers <table> @if (Session["ExhibitorData"] != null) { <tr> <td>Id</td> @*@{HomeController.Exhibitor exhibitor = ((HomeController.Exhibitor)(@Session["ExhibitorData"]))};*@ @*<td>@exhibitor.Id</td>*@ <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Id</td> </tr> <tr> <td>Name</td> <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Name</td> </tr> <tr> <td>surname</td> <td>@((HomeController.Exhibitor)(@Session["ExhibitorData"])).Surname</td> </tr> } </table>
When I tried to run my app and then click Aajax.ActionLink something gone wrong because after I clicked Ajax.ActionLink I go to different url. Home View:
After Clicked Ajax.ActionLink
What I want is to choose person name from dropdownlist, then click Ajax.ActionLink and get data of chosen person(exhibitor) without any redirect or refresh the site as I wrote at the beginning. Im also curious if it possible to do that only with one View- "Index" without using partialview.
原文:https://stackoverflow.com/questions/35149447
最满意答案
factorial *=i;
是指factorial = i * factorial;
事实变量范围是循环外的中继,因此它不会在每次循环迭代时重置
所以当i = 2时,时间因子= 2.0
当i = 3时,时间因子值是2.0,计算高于2.0因子= 2.0 * 3即6
当i = 4时,时间因子= 6 * 4 = 24.0
当i = 5时,时间因子= 24 * 5 = 120.0
factorial *=i;
means factorial =i*factorial;
factoial variable scope is relay outside of loop so it it not reset on every loop iteration
so when i=2 that time factorial = 2.0
when i=3 that time factorial value is 2.0 calculated above so factorial = 2.0*3 i.e 6
when i=4 that time factorial = 6*4 =24.0
when i=5 that time factorial = 24*5 = 120.0
相关问答
更多-
Java Compute C(n,k)和具有biginteger的factorial(Java Compute C(n,k) and factorial with biginteger)[2023-08-29]
首先,返回值必须是BigInteger ,因为C(1000,900)远远超过int的范围。 其次,您不需要单独的factorial()方法。 在迭代时进行除法将通过不创建过大的中间值来增加内存占用(以执行多个分区为代价,但即便如此,它实际上可能更快)。 像这样: static BigInteger cSelect(int x, int y) { BigInteger v = BigInteger.ONE; for (int i = x, j = 1; j <= y; i--, j++) ... -
Java阶乘输出(Java factorial output)[2022-04-02]
你计算阶乘,但你永远不会打印它: System.out.printf("%d! = %d\n ", num, fact, Factorial(num, fact)); 应该 System.out.printf("%d! = %d\n ", num, Factorial(num, fact)); 此外,您的Factorial函数不使用fact参数,因此您应该删除它,并在函数内声明一个局部变量。 最后,要求“你想要另一个因子”应该在顶级,而不是在Factorial函数内部。 您的代码也不使用用户输入的字符: ... -
这不意味着“1”一直在最后返回吗? 不,当N小于1时,它将仅返回1.(根据您的条件, if (N<=1 ) return 1; )对于所有其他情况,它将递归递归。 因为没有变量存储总和我不知道这是如何工作,它不会只是返回和丢失? 当一个方法返回时,它退出当前方法并返回到调用点并从那里继续 。 为简单起见,请采用以下方案:methodA调用methodB,methodB调用methodC: public void methodA(){ print("entering method A.."); / ...
-
循环的因子(Factorial for loop)[2023-01-22]
在循环外部移动打印语句? for(i in 1:x){ y <-y*((1:x)[i]) } print(y) Move the print statement outside the loop? for(i in 1:x){ y <-y*((1:x)[i]) } print(y) -
阶乘循环变为0(Factorial loop becomes 0)[2023-06-21]
从34开始,所有阶乘可以被2 ^ 32整除。 所以,当你的计算机程序计算模2 ^ 32的结果(虽然你没有说你正在使用什么编程语言,但这很可能),那么结果总是为0。 这是一个在Python中计算阶乘mod 2 ^ 32的程序: def sint(r): r %= (1 << 32) return r if r < (1 << 31) else r - (1 << 32) r = 1 for i in xrange(1, 40): r *= i print '%d! = %d ... -
while num > 1: factorial = factorial * num num = num - 1 while num > 1: factorial = factorial * num num = num - 1
-
分析Java中的阶乘循环(Analyzing factorial loop in Java)[2022-01-28]
factorial *=i; 是指factorial = i * factorial; 事实变量范围是循环外的中继,因此它不会在每次循环迭代时重置 所以当i = 2时,时间因子= 2.0 当i = 3时,时间因子值是2.0,计算高于2.0因子= 2.0 * 3即6 当i = 4时,时间因子= 6 * 4 = 24.0 当i = 5时,时间因子= 24 * 5 = 120.0 factorial *=i; means factorial =i*factorial; factoial variable ... -
因子递归(Factorial Recursion)[2021-10-29]
public static void Print(int n) { for (int i = n; i > 0; i--) { System.out.print(i); if (i == 1) { System.out.print("="); continue; } System.out.print("x"); } } 并输出: Enter an integer: 4 4! is ... -
我只想添加关于整数溢出的数学推理: 12! = 479,001,600 13! = 6,227,020,800 现在, int (32位)类型的范围限制是: -2,147,483,648 to 2,147,483,647 自从因子变为13以来超过了: 479,001,600 < 2,147,483,647 < 6,227,020,800 由于溢出,当你有13阶乘时,它将其视为: 13! = 6,227,020,800 % 4,294,967,296 = 1,932,053,504 + 4,2 ...
-
你快到了。 您在main中缺少一些代码: 提示用户输入数字。 您可以使用System.out.println 。 读取用户输入的int 。 您可以使用Scanner.nextInt 。 设置一个变量i的for循环,从0到用户输入的数字。 在循环体中调用System.out.println(i + "! = " + factorial(i)) 。 完成上述四个步骤后,您就完成了! You are almost there. You are missing some code in your main: Pro ...