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

时间:2011-02-10 11:29:50

标签: 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" 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)));
    }

问题是,如果我从控件中删除按钮它工作正常,但使用按钮它不会更新模型。 POST的参数在编辑表单中插入了值,但记录未保存到db。

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

由于

1 个答案:

答案 0 :(得分:1)

这不起作用的原因是因为只有POST HTTP谓词才能访问InsertTimeRegistration。你在javascript中调用它的方式是使用window.openwindow.showModalDialog,我想它们都发送一个GET请求(我确信window.open)。

因此,您需要配置window.showModalDialog函数以发送POST请求。以下是如何使用AJAX发布HTML <form>的示例:

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