我想用jQuery的對話框替換我現在在ASP.NET MVC應用程序中使用的一些代碼。舊代碼使用「提交」按鈕,使用不同的「值」設置。我想知道如果有更好的方法。如何使用jQuery對話框使用不同值的按鈕提交
這是視圖內現有的代碼的一個例子:
<%using (Html.BeginForm("SubmitValue", "MyController", FormMethod.Post))
{ %>
<span class="field-description">My Value: </span>
<input id="myTextValue" maxlength="6" name="myValue" type="text"/>
<%=Html.Hidden("myModel.myProperty1", Model.PropertyValue1) %>
<%=Html.Hidden("myModel.myProperty2", Model.PropertyValue2) %>
<div class="pageaction button">
<strong>
<input name="submitButton" type="submit" value="Save"/>
</strong>
<strong>
<input name="submitButton" type="submit" value="Delete"/>
</strong>
<strong>
<input type="button" value="Cancel" />
</strong>
</div>
<% } %>
控制器將需要按下提交按鈕的值:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SubmitValue(MyModel model, string submitButton)
{
_request.myProperty1= model.PropertyValue1;
_request.myProperty2= model.PropertyValue2;
if (submitButton == "Save")
{_myService.UpdateValue(_request);}
else
{_myService.DeleteValue(_request);}
return View("MyView", _request);
}
對於jQuery UI的,我喜歡使用代碼的「按鈕」部分來處理提交值。
$(function() {
$("#dialogWindow").dialog({
bgiframe: true,
autoOpen: false,
height: 200,
modal: true,
open: function(e, ui) {
$("#dialogWindow :input[name='myValue']").val(currentValue);
},
buttons: {
'Save': function() {
// TODO: submit as if a submit button with the value 'Save' is used
$(this).dialog('close');
},
'Delete': function() {
// TODO: submit as if a submit button with the value 'Delete' is used
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
},
close: function() {
//close code
}
});
});