繁体   English   中英

在Razor中向URL.Action添加参数

[英]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.

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