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

問題は、コントロールからボタンを削除しても問題ありませんが、ボタンを使用するとモデルが更新されないことです。POST のパラメーターには、編集フォームに挿入された値がありますが、レコードは db に保存されません。

できればアドバイスをお願いします。

ありがとう

4

1 に答える 1

1

これが機能しない理由はInsertTimeRegistration、POST HTTP 動詞でしかアクセスできないためです。あなたがjavascriptでそれを呼び出す方法は、どちらか、window.openまたはwindow.showModalDialog両方がGETリクエストを送信すると思われるものです(私はwindow.open.

window.showModalDialogそのため、POST リクエストを送信するように関数を構成する必要があります。<form>以下は、 AJAX を使用して HTML を投稿する方法の例です。

var formToPost = $('#idofyourform');
$.ajax({
    url: formToPost.attr('action'),
    type: 'POST',
    data: formToPost.serialize(),
    success: function(result) {
        alert('form successfully submitted');
    }
});
于 2011-02-11T07:32:28.537 に答える