ASP.NET MVC:模型绑定和 Ajax 请求
我有一个按钮:
<a id="2" class="modalInput specialbutton" href="/Employee/Delete/2" rel="#yesno"><img src="/Content/Images/application_delete.png" alt="Delete" /></a>
按钮的Javascript:
var buttons = $("#yesno button").click(function (e) {
var yes = buttons.index(this) === 0;
if (yes) {
$.ajax({
url: overlayElem.attr('href'),
success: function (data) {
$("#gridcontainer").html(data);
}
});
}
});
删除操作:
public ActionResult Delete(int id)
{
DeleteTeamEmployeeInput deleteTeamEmployeeInput = new DeleteTeamEmployeeInput { TeamEmployee = id };
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
问题是 id 参数。直接使用 DeleteTeamEmployeeInput
会很好。
public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput )
{
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
当我使用复杂对象时,它始终为空。简单的 int 类型工作得很好。
如何使用复杂类型进行删除操作?
类DeleteTeamEmployeeInput:
public class DeleteTeamEmployeeInput
{
public int TeamEmployee { get; set; }
}
删除按钮:
public static string DeleteImageButton(this HtmlHelper helper, int id)
{
string controller = GetControllerName(helper);
string url = String.Format("/{0}/Delete/{1}", controller, id);
return ImageButton(helper, url, "Delete", "/Content/Images/application_delete.png", "#yesno", "modalInput", id);
}
I have a button:
<a id="2" class="modalInput specialbutton" href="/Employee/Delete/2" rel="#yesno"><img src="/Content/Images/application_delete.png" alt="Delete" /></a>
Javascript for the button:
var buttons = $("#yesno button").click(function (e) {
var yes = buttons.index(this) === 0;
if (yes) {
$.ajax({
url: overlayElem.attr('href'),
success: function (data) {
$("#gridcontainer").html(data);
}
});
}
});
Delete Action:
public ActionResult Delete(int id)
{
DeleteTeamEmployeeInput deleteTeamEmployeeInput = new DeleteTeamEmployeeInput { TeamEmployee = id };
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
The problem is the id
parameter. It would be nice to use directly DeleteTeamEmployeeInput
.
public ActionResult Delete(DeleteTeamEmployeeInput deleteTeamEmployeeInput )
{
return Command<DeleteTeamEmployeeInput, TeamEmployee>(deleteTeamEmployeeInput,
s => RedirectToAction<EmployeeController>(x => x.Index(1)),
f => RedirectToAction<EmployeeController>(x => x.Index(1)));
}
When I use the complext object, it is always null. The simple int type works fine.
How can use a complex type for my delete action?
Class DeleteTeamEmployeeInput:
public class DeleteTeamEmployeeInput
{
public int TeamEmployee { get; set; }
}
Delete Button:
public static string DeleteImageButton(this HtmlHelper helper, int id)
{
string controller = GetControllerName(helper);
string url = String.Format("/{0}/Delete/{1}", controller, id);
return ImageButton(helper, url, "Delete", "/Content/Images/application_delete.png", "#yesno", "modalInput", id);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您肯定需要通过从点击回调中返回 false 来取消默认操作结果,否则您的 AJAX 请求甚至可能没有时间在重定向之前执行。就发送整个对象(仅包含一个
TeamEmployee
整数属性)而言,您可以执行以下操作:然后生成锚点,使其包含此参数:
现在您可以安全地拥有:
备注:锚点中的
id="2"
不是有效的标识符名称。You definitely need to cancel the default action result by returning false from your click callback or your AJAX request might not even have the time to execute before you get redirected. As far as sending the entire object (which contains only a single
TeamEmployee
integer propertry) is concerned you could do this:and then generate your anchor so that it includes this parameter:
Now you can safely have:
Remark:
id="2"
in your anchor is not valid identifier name.