首页 \ 问答 \ 如何在asp.net mvc中使用Ajax更新View?(How to update View using Ajax in asp.net mvc?)

如何在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之后

后Cliceked-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:Structure-Of-Folders 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:

Home-View

After Clicked Ajax.ActionLink

After-Cliceked-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
更新时间:2023-06-22 09:06

最满意答案

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

相关问答

更多
  • 首先,返回值必须是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++) ...
  • 你计算阶乘,但你永远不会打印它: 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.."); / ...
  • 在循环外部移动打印语句? 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)
  • 从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
  • 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 ...
  • 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 ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的