简体   繁体   English

在@ Url.Action中调用JavaScript函数作为参数

[英]Calling JavaScript function as parameter in @Url.Action

So I am trying to return a value from a text box as the parameter to another controller action that returns another partial. 所以我试图从文本框中返回一个值作为参数到另一个控制器操作,该操作返回另一个部分。 I think it would easier to just post some sample code rather than trying to explain what I am trying to do, so here is some sample code: 我认为只发布一些示例代码比尝试解释我要做什么会容易,所以这里有一些示例代码:

CSHTML: CSHTML:

<div class="row">
  <div class="pt-sm-30 pb-xs-30 has-50px-footer">
    <div class="col-lg-offset-1 col-lg-10">
        <h3>CREATE A NEW PERSON PROFILE</h3>            
        <form class="form-spacers">
            <div class="form-group">
                <div class="row">
                    <div class="col-md-6">
                        <label class="input-label" for="functionalRole">First Name <span class="ReqField">*</span></label>
                        @Html.TextBoxFor(model => model.Person.FirstName, new { @class = "form-control input-sm", @id = "firstName", @type = "text" })
                    </div>
                    <div class="col-md-6">
                        <label class="input-label" for="functionalRole">Last Name <span class="ReqField">*</span></label>
                        @Html.TextBoxFor(model => model.Person.LastName, new { @class = "form-control input-sm", @id = "lastName", @type = "text" })
                    </div>                        
                </div>
            </div>
        </form>
    </div>
</div>
<div class="row">
    <div class="col-sm-8 col-md-9 col-lg-10 new-profile-footer">
        <div class="col-lg-offset-1 col-lg-5 col-md-4 hidden-sm hidden-xs" style="margin-top: 16px;">
        </div>
        <div class="col-lg-6 col-md-8 col-sm-12" style="margin-top: 10px; text-align: right;">
            <div class="row" style="white-space: nowrap;">
                <button class="btn btn-primary button-blue btn-xs-110" onclick="location.href='@Url.Action("Index", "DirectoryMaintainer")'"><i class="fa fa-times-circle-o icon-xs-hidden" aria-hidden="true" style="padding-right: 5px;"></i>CANCEL</button>
                <button id="continue" type="button" class="btn btn-success button-green btn-xs-110">CONTINUE<i class="fa fa-caret-right icon-xs-hidden" style="padding-left: 5px;" aria-hidden="true"></i></button>
            </div>
        </div>
    </div>
</div>

<script>
    $("#continue").click(function () {
        $("#partialViewDiv").load('@(Url.Action("RecordsMatch", "DirectoryMaintainer", new { @firstName = getFirstName(), @lastName = getLastName()}, Request.Url.Scheme))');
    });

    function getFirstName() {
        return document.getElementById("firstName").value;
    }

    function getLastName() {
        return document.getElementById("lastName").value;
    }


</script>

Controller: 控制器:

    public PartialViewResult RecordsMatch(string firstName, string lastName)
    {
        //Do some logic with parameters here
        return PartialView("_RecordsMatch");
    }

So the issue I am having this that the line 所以我有这个问题

$("#partialViewDiv").load('@(Url.Action("RecordsMatch", "DirectoryMaintainer", new { @firstName = getFirstName(), @lastName = getLastName()}, Request.Url.Scheme))');

is giving me an error on getFirstName() and getLastName(). 在getFirstName()和getLastName()上给我一个错误。 The error is "The name getFirstName() does not exist in the current context". 错误是“名称getFirstName()在当前上下文中不存在”。 I am pretty new to MVC so I'm not sure if this is even possible or if there is a better way of doing it. 我对MVC还是很陌生,所以我不确定这是否可能,或者是否有更好的方法。 If there is, then I am more than happy to learn it. 如果有,那么我很乐于学习。 Any and all suggestions would be greatly appreciated. 任何和所有建议将不胜感激。

You cannot mix c# and js like that as the Url.Action gets executed in the server before your js code 您不能将C#和js混合使用,因为Url.Action在js代码之前在服务器中执行

Basically any C# code in your razor view gets executed by the razor view engine in the server and output of that (which is HTML/plain text) will be send to the browser. 基本上,您的剃刀视图中的任何C#代码都将由服务器中的剃刀视图引擎执行,并且该C#代码的输出(即HTML /纯文本)将发送到浏览器。 All your javascript code gets executed in the browser. 您所有的JavaScript代码都会在浏览器中执行。

You can use Url.Action to generate the base url (without route value parameters) and add query strings to that at client side later. 您可以使用Url.Action生成基本url(不Url.Action值参数),然后在客户端向其添加查询字符串。

$(function(){

  $("#continue").click(function () {

     var url='@Url.Action("RecordsMatch", "DirectoryMaintainer")';
     url = url+'?firstName='+ getFirstName()+'&lastName='+getLastName();

     $("#partialViewDiv").load(url);

  });

});

When razor executes this code, it will render output like this (you can check the page view source and see this) 当razor执行此代码时,它将呈现这样的输出(您可以查看页面查看源并看到此内容)

$(function(){

  $("#continue").click(function () {

     var url='/DirectoryMaintainer/RecordsMatch';
     url = url+'?firstName='+ getFirstName()+'&lastName='+getLastName();

     $("#partialViewDiv").load(url);

  });

});

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

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