[英]Adding Parameters to URL.Action in Razor
大家好當前,我有一個帶有按鈕和一些javascript的網站,它們會創建一個加載外觀,然后運行此actionresult。 我想向actionresult添加參數,但不確定如何執行。 謝謝! 這是我的代碼控制器:
[HttpPost]
public ActionResult PostMethod(string MyText)
{
System.Threading.Thread.Sleep(5000);
return Json("And were done");
}
視圖:
<input type="text" name="MyTextBlock"/>
<p id="PID">
Default message from declarative syntax.
</p>
<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px;
top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001;
opacity: .8; filter: alpha(opacity=70);display:none" >
<p style="position: absolute; top: 30%; left: 45%; color: White;" align="center">
<img src="../../Content/themes/base/images/ajax-loading.gif"><br />
Loading, please wait...
</p>
</div>
<button onclick="JavascriptFunction();">HTTPPost Button</button>
<script type="text/javascript" language="javascript">
function JavascriptFunction() {
var url = '@Url.Action("PostMethod", "MyTextBlock", new { MyText = "john" })';
$("#divLoading").show();
$.post(url, null,
function (data) {
$("#PID")[0].innerHTML = data;
$("#divLoading").hide();
});
}
</script>
我想要做的是將MyTextBox傳遞到PostMethod中以用作MyText。 我還看到了一些其他示例,這些示例中的硬編碼是我希望來自文本框的值。 任何幫助是極大的贊賞。 謝謝!
剃須刀是在頁面加載之前生成的,因此,如果要在頁面加載之后使用文本框,則需要使用javascript(也就是說,最終用戶是否要更改MyTextBox
的值,並且要使用AJAX傳遞此新值)。
不用在$.post
中將null
作為數據參數傳遞,而是在這里獲取MyTextBox
的值並將其傳遞給操作。 例如:
var url = '@Url.Action("PostMethod")';
var data = $('input[name="MyTextBox"]').serialize();
$.post(url, data, function(data){
});
看來您正在嘗試編寫許多MVC已經為您處理的代碼。 試試看...
首先,為您的視圖創建一個模型。 您可以隨便叫這個。 在此處將所需的屬性作為參數放入操作方法中。
YourModel.cs
public class YourModel
{
public string MyText { get;set; }
}
對於您的控制器,您必須更改兩件事。 頁面的GET操作將需要一個模型傳遞給它,如下所示。
對於POST操作,將string MyText
參數更改為YourModel model
。 這將使MVC將您在視圖上的輸入綁定到模型。
行動方法
public class YourController
{
[HttpGet]
public ActionResult PostMethod()
{
YourModel model = new YourModel();
return View(model);
}
[HttpPost]
public ActionResult PostMethod(YourModel model)
{
//Do something with model.MyText;
System.Threading.Thread.Sleep(5000);
return Json("And We're Done");
}
}
PostMethod.cshtml
//THIS LINE HERE IS THE MOST IMPORTANT
@model YourNamespace.YourModel
//Ajax will handle most of the calling and showing of your elements if you tell it what to do!
@using(Ajax.BeginForm(new AjaxOptions(){ LoadingElementId="divloading", OnSuccess="OnSuccessFunction" }))
{
<input type="text" name="MyText"/>
//Quick note: If you decide to hand code your html, make sure your <input/> name attributes match the property names on your model object.
//Or here you could do @Html.TextBoxFor(m => m.MyText)
<p id="PID">
Default message from declarative syntax.
</p>
<div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px;
top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001;
opacity: .8; filter: alpha(opacity=70);display:none" >
<p style="position: absolute; top: 30%; left: 45%; color: White;" align="center">
<img src="../../Content/themes/base/images/ajax-loading.gif"><br />
Loading, please wait...
</p>
</div>
<input type="submit" name="Submit" value="HTTPPost Button"/>
<script type="text/javascript" language="javascript">
function OnSuccessFunction(data, textStatus, jqXHR){
$("#PID")[0].innerHtml = data;
}
</script>
}
現在,如果您向模型添加更多屬性,則不必更改JS,這樣做的好處是。 您只需使用HtmlHelper向視圖添加另一個輸入,或手動輸入名稱屬性等於模型上屬性名稱的輸入名稱。 MVC將處理其余的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.