twitter-bootstrap URL.Action 参数中的客户端值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18232491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 17:58:11  来源:igfitidea点击:

Client side value in URL.Action parameter

asp.net-mvc-4twitter-bootstrapurl.action

提问by callisto

I am trying to post feedback on an object from a modal popup. No values on the popup are populated from server side.

我正在尝试从模态弹出窗口中发布有关对象的反馈。弹出窗口中没有值是从服务器端填充的。

 <div class="modal fade" id="msg-editor">
        <div class="modal-header">
            <a class="close" data-dismiss="modal">&times;</a>
            <h3>Title of the form</h3>
        </div>
        <div class="modal-body">
            <div class="row-fluid">
                <div class="controls span10">
                    <label class="control-label" for="Title">Title and message</label>
                    <input type="text"
                        class="input-xlarge" id="Title" name="Title"
                        placeholder="Title of the message" />
                    <textarea class="input-xlarge" id="Message"
                        name="Message" rows="5" cols="9"
                        placeholder="Message"></textarea>

                    <label class="checkbox">
                        <input type="checkbox" value="option2" id="EmailSelf">
                        Send a copy of this message to yourself
                    </label>

                </div>
            </div>
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">Close</a>
            <a id="Submit" class="btn btn-primary" href="@Url.Action("AddDocumentMessage", "Invoice", new { param1 =  $('#myFormSubmit').value, param2 = Model.Obj.Value1, param3 = Model.HashedValue })">Send</a>
        </div>
    </div>

The part that doesnt work is

不起作用的部分是

param1 = $('#myFormSubmit').value.

param1 = $('#myFormSubmit').value。

How do I pass the client side value?

如何传递客户端值?

回答by Amin Saqi

You can't...!

你不能……!

Because razor code is parsed and rendered in the server side, and in server, there is no jquery or any client code or data...

因为razor代码是在服务器端解析渲染的,在服务器端,没有jquery或者任何客户端代码或数据...

An alternative similar work can be like the following:

另一种类似的工作可能如下所示:

<div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <input type="button" value="Send" onclick="submit()" />
</div>

<script>
    function submit() {
        $.ajax({
            url: @Url.Action("act", "con") + '?param1=' + $('#myFormSubmit').value +
                                              '&param2=' @Model.Obj.Value1 + 
                                              '&param3=' + @Model.HashedValue,
            type: 'POST',
            // ... other ajax options ...
        });
    }
</script>

Then, in your action method you'll receive all params in string:

然后,在您的操作方法中,您将收到字符串中的所有参数:

[HttpPost]
public ActionResult act(string param1, string param2, string param3)
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }

回答by asymptoticFault

The problem you are running into is that the Razor code is executed on the server to create the URL and then sent to the client, so the client side value is not known at the time the URL is created. You need to create a placeholder in the URL so that the client side value can be inserted when the document is ready.

您遇到的问题是 Razor 代码在服务器上执行以创建 URL,然后发送到客户端,因此在创建 URL 时客户端值是未知的。您需要在 URL 中创建一个占位符,以便在文档准备好时可以插入客户端值。

So something like this:

所以像这样:

<a id="Submit" class="btn btn-primary" href="@Url.Action("AddDocumentMessage", "Invoice", new { param1 = "param1_placeholder" , param2 = Model.Obj.Value1, param3 = Model.HashedValue })">Send</a>

$(function () {

    var url = $('#Submit').attr('href');
    $('#Submit').attr('href', url.replace('param1_placeholder', $('#myFormSubmit').value));

});

回答by Andriy Gubal

You may try to generate and append the link you need:

您可以尝试生成并附加您需要的链接:

<div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">Close</a>
<div class="link"></div>
        </div>


$(function () {
var path ="@Url.Action("AddDocumentMessage", "Invoice",new {param2 = Model.Obj.Value1, param3 = Model.HashedValue })

$('.link').append('<a id="Submit" class="btn btn-primary" href="'+path+'&param1='+$('#myFormSubmit').val()+'">Send</a>')

});