【问题标题】:Mvc textbox assigning to the other variable?Mvc文本框分配给另一个变量?
【发布时间】:2014-01-04 20:47:18
【问题描述】:

我是 mvc 新手,我想知道如何将文本框文本分配给 mvc 中的另一个变量。

在 windows 中我们可以使用 textbox1.text = var body;

我怎样才能使用下面的代码来做类似的事情

我想在 textboxfor 中写入文本,以便调用 PAF 控制器索引操作的字符串邮政编码参数?

  @Html.TextBoxFor(model => model.PostCode)

          **Here we will get : postcode: ------------(Assume Postcode:52345)**

  <input type="button" name="postcode" value="check your address" onclick="location.href='@Url.Action("Index", "PAF", new {Postcode = ...... )})'" />

            **Here we will get : check your address button**

这是我的 PAF 控制器索引操作

public class PAFController : Controller
{
     public ActionResult Index(string Postcode)
     {
        return View();
     }
}

【问题讨论】:

标签: asp.net-mvc asp.net-mvc-4 razor html-helper html.textbox


【解决方案1】:

你需要做一些事情,比如

onclick="location.href='@Url.Action("Index", "PAF")?Postcode=' + $('#postcode').val()"

假设您为输入文本框提供邮政编码的 ID:

@Html.TextBoxFor(model => model.PostCode, new { id = "postcode" });

【讨论】:

  • 好吧!看起来不错..通过 id 调用文本框值.. 什么?Postcode=' 表示我可以直接在代码本身中使用那个值,还是可以为 jquery 使用脚本标记?
  • 这只是 url 的一部分,例如 test.com/PAF/Index?Postcode=3 如果您希望 url 以这种方式格式化。我确定你需要调整它,但你就是这样做的
【解决方案2】:

这里我得到了上述问题的答案:我使用了 Jquery Ajax

 ** added data-attribute to the button**

    <input type="button" data-paf-url="/paf/index" id="postcodebutton" 
       value="check your address" /> 

    **added Id for the textbox**
   @Html.TextBoxFor(model => model.PostCode, 
       new { @class = "form-control",id="postcode",   placeholder="Postcode" })

<script>
$(document).ready(function () {
    $("#postcodebutton").click(function (e) {
        e.preventDefault();
        var postc= $("#postcode").val();
        $.ajax({
            url: $(this).attr('data-paf-url'),
            type: 'GET',
            data:{Postcode:postc},
            success: function (data, textStatus, jqXHR) {

               // my code
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert('An error occured status' + textStatus + 'error thrown:' + errorThrown);
            }
        });

    });
});
</script>

【讨论】:

    猜你喜欢
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多