C# MVC4和jquery:使用按钮重定向到另一个视图,并发送参数对象(到另一个视图)
首先,我必须说我是MVC4新手,同时学习和开发这个应用程序。 在这个应用程序中有“index.cshtml”视图,它包含“fresh”span(按钮)。 单击此按钮后,我希望:C# MVC4和jquery:使用按钮重定向到另一个视图,并发送参数对象(到另一个视图),c#,jquery,asp.net-mvc-4,razor,C#,Jquery,Asp.net Mvc 4,Razor,首先,我必须说我是MVC4新手,同时学习和开发这个应用程序。 在这个应用程序中有“index.cshtml”视图,它包含“fresh”span(按钮)。 单击此按钮后,我希望: 要重定向到另一个名为“面试”的视图 还希望发送参数“数据”以查看(控制器操作方法) Index.cshtml查看代码片段: . . some code <span class="action-class" id="fresh">Start Fresh Interview</span> . . so
.
.
some code
<span class="action-class" id="fresh">Start Fresh Interview</span>
.
.
some code
.
.
$("#fresh").click(function (e) {
var tech;
var guid = '@Model.currentInterviewDetails.GuId';
$.ajax(
{
type: "post",
url: '@Url.Action("Interview","Home")',
data: {
GuId: guid,
IntervieweeName: $("#IntervieweName").val(),
LevelId: $("#SelectedLevelId").val(),
DId: $("#SelectedDesignationId").val(),
TrackId: $("#SelectedTrackId").val(),
TechId: tech
},
success: function (data) {
}
});
});
下面是控制器代码:
@model InterviewAssistant.Models.CommonWrapper
@{
ViewBag.Title = "Interview";
}
@
@if (Model.tech != null && Model.tech.Count() > 0)
{
@Html.HiddenFor(m => m.SelectedTrackId)
@Html.DropDownListFor(
m => m.SelectedTechId,
new SelectList(Model.tech, "TechId", "TechName"),
string.Empty
)
}
public ActionResult Interview(InterviewDetailsModel interview)
{
string name = System.Web.HttpContext.Current.User.Identity.Name;
CommonWrapper wrapper = new CommonWrapper();
wrapper.track = CommonWrapper.GetTracks();
wrapper.level = CommonWrapper.GetLevel();
wrapper.designation = CommonWrapper.GetDesignation();
wrapper.currentInterviewDetails = interview;
wrapper.tech = (from s in CommonWrapper.GetTechnology()
where s.TrackId == interview.TrackId
orderby s.TechName
select s).ToList();
return View("Interview", wrapper);
}
有谁能帮我解决这个问题吗
提前感谢:-)HTML帮助程序 为什么不尝试使用一些Razor HTML帮助程序呢?如果您愿意,您可以随时使用jquery,但是如果您使用的是MVC,那么您可能会利用它的一些核心特性!如果您的参数名为“面试”,请执行以下操作:
@Html.ActionLink("Click me","Interview","ControllerName",new {interview= Model.currentInterviewDetails.GuId},null)
其中数据是作为参数发送的内容,访谈是视图名称,ControllerName是控制器名称,空值表示可选的HTML参数。如果您有多个参数,如名称、数据和年龄,则如下所示:
@Html.ActionLink("Click me","Interview","ControllerName",new {name = "Glitch100",data = 4, age = 22},null)
这就是MVC4的强大之处——不用担心自己制作链接。如果您想对其进行AJAX调用,只需使用:
@Ajax.ActionLink("Click me","Interview","ControllerName", new {data= 4}, new AjaxOptions {HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "myIdToUpdate", OnComplete = "runMeOnComplete()"})
BeginForm
.
.
some code
<span class="action-class" id="fresh">Start Fresh Interview</span>
.
.
some code
.
.
$("#fresh").click(function (e) {
var tech;
var guid = '@Model.currentInterviewDetails.GuId';
$.ajax(
{
type: "post",
url: '@Url.Action("Interview","Home")',
data: {
GuId: guid,
IntervieweeName: $("#IntervieweName").val(),
LevelId: $("#SelectedLevelId").val(),
DId: $("#SelectedDesignationId").val(),
TrackId: $("#SelectedTrackId").val(),
TechId: tech
},
success: function (data) {
}
});
});
如果您需要传递更多信息,为什么不使用表单呢?最好还是让MVC使用您的模型为您构建表单!还有两种类型——Html和AJAX
HTML BeginForm
属性到您的控制器
重载:
@model InterviewAssistant.Models.CommonWrapper
@{
ViewBag.Title = "Interview";
}
@
@if (Model.tech != null && Model.tech.Count() > 0)
{
@Html.HiddenFor(m => m.SelectedTrackId)
@Html.DropDownListFor(
m => m.SelectedTechId,
new SelectList(Model.tech, "TechId", "TechName"),
string.Empty
)
}
public ActionResult Interview(InterviewDetailsModel interview)
{
string name = System.Web.HttpContext.Current.User.Identity.Name;
CommonWrapper wrapper = new CommonWrapper();
wrapper.track = CommonWrapper.GetTracks();
wrapper.level = CommonWrapper.GetLevel();
wrapper.designation = CommonWrapper.GetDesignation();
wrapper.currentInterviewDetails = interview;
wrapper.tech = (from s in CommonWrapper.GetTechnology()
where s.TrackId == interview.TrackId
orderby s.TechName
select s).ToList();
return View("Interview", wrapper);
}
相关:
@model InterviewAssistant.Models.CommonWrapper
@{
ViewBag.Title = "Interview";
}
@
@if (Model.tech != null && Model.tech.Count() > 0)
{
@Html.HiddenFor(m => m.SelectedTrackId)
@Html.DropDownListFor(
m => m.SelectedTechId,
new SelectList(Model.tech, "TechId", "TechName"),
string.Empty
)
}
public ActionResult Interview(InterviewDetailsModel interview)
{
string name = System.Web.HttpContext.Current.User.Identity.Name;
CommonWrapper wrapper = new CommonWrapper();
wrapper.track = CommonWrapper.GetTracks();
wrapper.level = CommonWrapper.GetLevel();
wrapper.designation = CommonWrapper.GetDesignation();
wrapper.currentInterviewDetails = interview;
wrapper.tech = (from s in CommonWrapper.GetTechnology()
where s.TrackId == interview.TrackId
orderby s.TechName
select s).ToList();
return View("Interview", wrapper);
}
用简单的方法,您可以制作
控制器
public class DefaultController : Controller
{
Public ActionResult Index()
{
Model model = new Model();
model.TechId = "TechId here";
model.TechName = "TechName";
return View(model);
}
public ActionResult InterView()
{
return View();
}
[HttpPost]
public ActionResult InterView(Model model)
{
//retrive model here
//return your interview view here
return View("InterView");
}
}
在Index.cshtml视图中:
@model namespace.model.modelname
@using (Html.BeginForm("InterView", "Default", FormMethod.Post))
{
<input type="submit" name="SubmitInterview" value="Submit" />
}
@model namespace.model.modelname
@使用(Html.BeginForm(“面试”、“默认”、FormMethod.Post))
{
}
但此选项将在该页面中呈现我的视图。如果使用AJAX选项,则为“是”,但如果使用HTML.ActionLink,则会将您重定向到该页面。是否要将该Interview.cshtml呈现到屏幕上?然后使用一个Ajax请求。此外,将[HttpPost]属性添加到控制器中。是的,我想将Interview.cshtml呈现到屏幕上。我将[HttpPost]添加到控制器中,但它仍然没有加载“interview.cshtml”。然后执行我建议的操作-一个Ajax.ActionLink-调用您的控制器,使用HttpPost,并为要呈现部分的页面指定一个ID。表单提交的模型和HttpPost方法中捕获的值不同步。编辑和更改it@Reddy我听不懂你的评论,你能解释清楚吗?:)对不起,我的英语很差,我打字很快,没有意识到错误。没问题,我已经纠正了上面的问题,谢谢