C# 如果单击“添加”按钮,编辑模式下的ASP.NET MVC ViewUserControl将不起作用

C# 如果单击“添加”按钮,编辑模式下的ASP.NET MVC ViewUserControl将不起作用,c#,asp.net-mvc,asp.net-mvc-2,telerik-grid,telerik-mvc,C#,Asp.net Mvc,Asp.net Mvc 2,Telerik Grid,Telerik Mvc,Hy, 我有以下ViewUserControl: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <table> <tr> <td><%= Html.TextBox("") %></td> <td><button type="button" id="workType-open-button" clas

Hy, 我有以下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>
在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)));
    }
问题是,如果我从控件中删除按钮,它可以正常工作,但使用按钮它不会更新模型。POST的参数具有插入编辑表单中的值,但记录不会保存到db

如果可以的话,请给我一个建议


谢谢

这不起作用的原因是您的InsertTimeRegistration只能通过POST HTTP谓词访问。在javascript中调用它的方式是使用window.open或window.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');
    }
});
var formToPost = $('#idofyourform');
$.ajax({
    url: formToPost.attr('action'),
    type: 'POST',
    data: formToPost.serialize(),
    success: function(result) {
        alert('form successfully submitted');
    }
});