如果添加按钮,ASP.NET MVC ViewUserControl 在编辑模式下不起作用
嘿, 我有以下 ViewUserControl:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<table>
<tr>
<td><%= Html.TextBox("") %></td>
<td><button type="button" id="workType-open-button" class="t-button" onclick="modalWin('http://localhost:29357/WorkType/SelectForTR')">Select</button></td>
</tr>
</table>
<script type="text/javascript">
function modalWin(url) {
if (window.showModalDialog) {
window.showModalDialog(url, "Select work type", "dialogWidth:700px;dialogHeight:450px");
}
else {
window.open(url, 'Select work type', 'height=700,width=450,toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no ,modal=yes');
}
}
</script>
我用它来编辑类的下一个属性:
[UIHint("WorkTypes"), Required]
public int WorkType { get; set; }
在 UserControl 中我有以下代码:
[HttpPost]
[GridAction]
public ActionResult InsertTimeRegistration()
{
TimeRegistrationViewModel newT = new TimeRegistrationViewModel();
if (TryUpdateModel(newT))
{
//The model is valid - insert the time registration.
newT.Employee = 6;
repo.AddTimeRegistration(newT);
}
return View(new GridModel(repo.GetAllTimeRegistrationOfAnEmployee(6)));
}
问题是,如果我从控件中删除按钮,它可以正常工作,但使用按钮就不行了` t 更新模型。 POST 的参数已将值插入到编辑表单中,但记录未保存到数据库中。
如果可以的话请给我建议。
谢谢
Hy,
I have the following ViewUserControl:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<table>
<tr>
<td><%= Html.TextBox("") %></td>
<td><button type="button" id="workType-open-button" class="t-button" onclick="modalWin('http://localhost:29357/WorkType/SelectForTR')">Select</button></td>
</tr>
</table>
<script type="text/javascript">
function modalWin(url) {
if (window.showModalDialog) {
window.showModalDialog(url, "Select work type", "dialogWidth:700px;dialogHeight:450px");
}
else {
window.open(url, 'Select work type', 'height=700,width=450,toolbar=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no ,modal=yes');
}
}
</script>
I use this to edit the next property of a class:
[UIHint("WorkTypes"), Required]
public int WorkType { get; set; }
In the UserControl i have the following code:
[HttpPost]
[GridAction]
public ActionResult InsertTimeRegistration()
{
TimeRegistrationViewModel newT = new TimeRegistrationViewModel();
if (TryUpdateModel(newT))
{
//The model is valid - insert the time registration.
newT.Employee = 6;
repo.AddTimeRegistration(newT);
}
return View(new GridModel(repo.GetAllTimeRegistrationOfAnEmployee(6)));
}
The problem is that if i remove the button from the control it is work ok, but with button it don`t update the model. The parameter of the POST have the value inserted in the edit form, but the record isn't saved to db.
Please give me an advice if you can.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不起作用的原因是您的
InsertTimeRegistration
只能通过 POST HTTP 动词访问。您在 javascript 中调用它的方式是使用window.open
或window.showModalDialog
,我想它们都会发送 GET 请求(我确信对于window .open
)。因此,您需要配置
window.showModalDialog
函数来发送 POST 请求。以下是如何使用 AJAX 发布 HTMLThe reason this doesn't work is because your
InsertTimeRegistration
is accessible only with the POST HTTP verb. The way you are calling it in javascript is with eitherwindow.open
orwindow.showModalDialog
which I suppose both send a GET request (I am sure for thewindow.open
).So you need to configure your
window.showModalDialog
function to send a POST request. Here's an example of how you could post an HTML<form>
using AJAX: