简体   繁体   English

从文本框中获取传递给我的控制器的变量

[英]get a variable passed to my controller from a textbox

I have a simple model I am using for a search page to do some validation: 我有一个用于搜索页面的简单模型来进行一些验证:

        public class Search {
        [Required]
        [DisplayName("Tag Number")]
        [RegularExpression("([1-9][0-9]*)", ErrorMessage = "Tag must be a number")]
        public int HouseTag { get; set; }

i then have a simple view with a textbox and a submit button: 然后,我有一个带有文本框和提交按钮的简单视图:

@model Search

@{
    Layout = "~/_Layout.cshtml";
}   

@using (Html.BeginForm("Search", "Inquiry", FormMethod.Get)){
    @Html.LabelFor(m =>m.HouseTag)
    @Html.TextBoxFor(m=>m.HouseTag, new { type = "Search", autofocus = "true", style = "width: 200px", @maxlength = "6" })

    <input type="submit" value="Search" id="submit"/>

my controller is expecting a parameter of an id: 我的控制器期望一个id参数:

    [HttpGet]
    public ActionResult Search(int id){
        ViewBag.Tag = id;
        return View();
    }

when i execute it with a number i get a null value being passed to the controller, causing things to blow up. 当我用数字执行它时,我将一个空值传递给控制器​​,从而导致事情崩溃。 I am using the model to control some of the properties of the search box for validation. 我正在使用模型来控制搜索框的某些属性以进行验证。 I used to just have @Html.TextBox and it returned fine, but now that ive added the model, it doesnlt return anything. 我曾经只有@ Html.TextBox,它返回的很好,但是现在我已经添加了模型,它不会返回任何东西。

You can set your parameter to a type of Search and then access the property in your action 您可以将参数设置为“搜索”类型,然后在操作中访问该属性

[HttpGet]
public ActionResult Search(Search model){
    ViewBag.Tag = model.HouseTag;
    return View();
}

If it were me I'd make this a HttpPost or create a seperate action for this form so I wouldn't see the HouseTag text in the URL.. 如果是我,则将其设为HttpPost或为此表格创建单独的操作,这样我就不会在URL中看到HouseTag文本。

@using (Html.BeginForm("Search", "Inquiry", FormMethod.Post))
{
    @Html.LabelFor(m => m.HouseTag)
    @Html.TextBoxFor(m => m.HouseTag, new { type = "Search", autofocus = "true", style = "width: 200px", @maxlength = "6" })

    <input type="submit" value="Search" id="submit" />
}

[HttpPost]
public ActionResult Search(Search model){
    ViewBag.Tag = model.HouseTag;
    return View();
}

您期望使用一个名为id的参数,并将HouseTag作为该参数的名称传递,您应该在Search方法中将id重命名为houseTag。

There's a couple of things going on here. 这里发生了几件事。 First you are going to want to split your Get and Post actions. 首先,您将要拆分Get和Post操作。 Also forms are only used in conjunction with POST's. 表格也只能与POST一起使用。 You also don't need to name your action or controller unless you are sending the post to a different controller or action then the GET. 您也无需命名操作或控制器,除非您将帖子发送给另一个控制器或操作,然后再发送给GET。

This is the get. 这就是得到。 It renders the form on the page. 它在页面上呈现表单。 You don't need to put [HttpGet] on there, it is the default. 您不需要在其中放置[HttpGet],这是默认设置。

    public ActionResult Search()
    {
        return View();
    }

The following is going to post the form back to the server. 下面将把表单发回到服务器。 the model binder will wire up the html form fields with your view model. 模型资料夹将用您的视图模型连接html表单字段。 since you have validators on the view model, you'll want to check that the model state is valid and re-show the view with the associated errors. 由于您在视图模型上具有验证器,因此需要检查模型状态是否有效,然后重新显示带有相关错误的视图。 You will need to add an @Html.ValidationMessageFor(...) into your view so that you actually see those errors. 您将需要在视图中添加@ Html.ValidationMessageFor(...),以便实际看到这些错误。

    [HttpPost]
    public ActionResult Inquiry(Search search)
    {
        if (!ModelState.IsValid)
        {
            return View(search);
        }

        //so something with your posted model.
    }

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

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