如果添加按钮,ASP.NET MVC ViewUserControl 在编辑模式下不起作用

发布于 2024-10-16 18:55:38 字数 1541 浏览 4 评论 0原文

嘿, 我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-10-23 18:55:38

这不起作用的原因是您的 InsertTimeRegistration 只能通过 POST HTTP 动词访问。您在 javascript 中调用它的方式是使用 window.openwindow.showModalDialog ,我想它们都会发送 GET 请求(我确信对于 window .open)。

因此,您需要配置 window.showModalDialog 函数来发送 POST 请求。以下是如何使用 AJAX 发布 HTML

的示例:

var formToPost = $('#idofyourform');
$.ajax({
    url: formToPost.attr('action'),
    type: 'POST',
    data: formToPost.serialize(),
    success: function(result) {
        alert('form successfully submitted');
    }
});

The 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 either window.open or window.showModalDialog which I suppose both send a GET request (I am sure for the window.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:

var formToPost = $('#idofyourform');
$.ajax({
    url: formToPost.attr('action'),
    type: 'POST',
    data: formToPost.serialize(),
    success: function(result) {
        alert('form successfully submitted');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文