简体   繁体   English

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

[英]ASP.NET MVC ViewUserControl in edit mode doesn`t work if a add a button

Hy, I have the following 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>

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: 在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)));
    }

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. POST的参数在编辑表单中插入了值,但记录未保存到db。

Please give me an advice if you can. 请给我一个建议。

Thanks 谢谢

The reason this doesn't work is because your InsertTimeRegistration is accessible only with the POST HTTP verb. 之所以不起作用,是因为您的InsertTimeRegistration仅可通过POST HTTP动词访问。 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 ). 您在javascript中调用它的方式是使用window.openwindow.showModalDialog ,我想它们都发送GET请求(我肯定是window.open )。

So you need to configure your window.showModalDialog function to send a POST request. 因此,您需要配置window.showModalDialog函数以发送POST请求。 Here's an example of how you could post an HTML <form> using AJAX: 这是一个如何使用AJAX发布HTML <form>的示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM