大家好当前,我有一个带有按钮和一些javascript的网站,它们会创建一个加载外观,然后运行此actionresult。我想向actionresult添加参数,但不确定如何执行。谢谢!这是我的代码
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>

我要做的是将MyTextBox传递到PostMethod中以用作MyText。我还看到了一些其他示例,这些示例中的硬编码是我希望来自文本框的值。任何帮助是极大的赞赏。谢谢!

最佳答案

似乎您正在尝试编写许多MVC已经为您处理的代码。试试看...

首先,为您的 View 创建一个模型。您可以随便叫这个。在此处将所需的属性作为参数放入操作方法中。

YourModel.cs

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

对于您的 Controller ,您必须更改两件事。该页面的GET操作将需要一个模型传递给它,如下所示。

对于POST操作,请将string MyText参数更改为YourModel model。这将使MVC将您在 View 上的输入绑定(bind)到模型。

操作方法
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向 View 添加另一个输入,或者手动输入名称属性等于模型上属性名称的输入名称。 MVC将处理其余的工作。

10-07 19:53
查看更多