我有一个简单的表格,下拉列表中有人名(参展商)。在我选择其中一个并点击“获取参展商数据”链接后,我想只更新我的主页网站的一部分并显示所选参展商的数据。 项目中我的文件夹的结构: 我的家庭控制器如下所示:
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; }
}
}
}
我的首页文件夹中的索引视图代码如下所示:
@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-&#34; Index&#34;不使用partialview。
答案 0 :(得分:0)
正如Stephen Muecke所说,您应该在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"
},
};
//here how you pass model to View
ViewData.Model = exhibitors.FirstOrDefault(x => x.Id == Id);
return PartialView("_Exhibitor");
}
您的观点将是:
@model WebApplication5.Controllers.Exhibitor
@using WebApplication5.Controllers
<table>
@if (Model != null)
{
<tr>
<td>Id</td>
<td>@Model.Id</td>
</tr>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>surname</td>
<td>@Model.Surname</td>
</tr>
}
</table>
我所做的是使用@model WebApplication5.Controllers.Exhibitor
强力输入您的视图,然后使用模型属性显示数据。
答案 1 :(得分:0)
最后我找到了解决方案。 “问题是由于腐败的unobtrusive-ajax.min.js文件造成的。”我发现帖子asp.net mvc partialview @Ajax.ActionLink doesn't work,我遇到了同样的问题。我卸载了unobtrusive-ajax.min.js并再次安装它。在我的情况下这是很好的解决方案,现在ajax正常工作。