简体   繁体   English

在Razor中向URL.Action添加参数

[英]Adding Parameters to URL.Action in Razor

Hi all Currently I have a website that has a button and some javascript that creates a loading look and then runs this actionresult. 大家好当前,我有一个带有按钮和一些javascript的网站,它们会创建一个加载外观,然后运行此actionresult。 I want to add parameters to actionresult but not sure how to do it. 我想向actionresult添加参数,但不确定如何执行。 Thanks! 谢谢! Here is my code Controller: 这是我的代码控制器:

    [HttpPost]
    public ActionResult PostMethod(string MyText)
    {
        System.Threading.Thread.Sleep(5000);

        return Json("And were done");
    }

View: 视图:

<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>

What I want to do is pass MyTextBox into PostMethod to use it as MyText. 我想要做的是将MyTextBox传递到PostMethod中以用作MyText。 Some of the other examples I have seen hardcode in values where I want it to be from the textbox. 我还看到了一些其他示例,这些示例中的硬编码是我希望来自文本框的值。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

Razor is generated before page load, so if you want a textbox after the page is loaded you need to use javascript (that is to say if the end-user will be changing the value of MyTextBox and you want to pass this new value using AJAX). 剃须刀是在页面加载之前生成的,因此,如果要在页面加载之后使用文本框,则需要使用javascript(也就是说,最终用户是否要更改MyTextBox的值,并且要使用AJAX传递此新值)。

Instead of passing null as your data argument in $.post , this is where you would grab the value of MyTextBox and pass it to the action. 不用在$.post中将null作为数据参数传递,而是在这里获取MyTextBox的值并将其传递给操作。 For example: 例如:

var url = '@Url.Action("PostMethod")';
var data = $('input[name="MyTextBox"]').serialize();
$.post(url, data, function(data){

});

It seems like you're trying to hand code a lot of what MVC already handles for you. 看来您正在尝试编写许多MVC已经为您处理的代码。 Try this out... 试试看...

First out, create a model for your view. 首先,为您的视图创建一个模型。 You can call this whatever you want. 您可以随便叫这个。 Put the properties that you want as your parameters to your action method in here. 在此处将所需的属性作为参数放入操作方法中。

YourModel.cs YourModel.cs

public class YourModel
{
    public string MyText { get;set; }
}

For your controller, you'll have to change two things. 对于您的控制器,您必须更改两件事。 The GET action for the page will need a model passed to it, like shown below. 页面的GET操作将需要一个模型传递给它,如下所示。

For the POST action, change your string MyText parameter to YourModel model . 对于POST操作,将string MyText参数更改为YourModel model This will allow MVC to bind your inputs on your view to the model. 这将使MVC将您在视图上的输入绑定到模型。

Action Method 行动方法

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

Some benefits of doing it this way is now your JS doesn't have to change if you add more properties to your model. 现在,如果您向模型添加更多属性,则不必更改JS,这样做的好处是。 You just add another input to your view using either the HtmlHelper or hand code the input name with the name attribute equal to the name of the property on your model. 您只需使用HtmlHelper向视图添加另一个输入,或手动输入名称属性等于模型上属性名称的输入名称。 MVC will handle the rest. MVC将处理其余的工作。

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

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