【问题标题】:How do I get the value of an input of html.textbox in ASP.NET MVC 2如何在 ASP.NET MVC 2 中获取 html.textbox 的输入值
【发布时间】:2010-07-12 03:50:31
【问题描述】:

我目前有一个TextBox 使用:

<%: Html.TextBox("TextBox1") %>

如何获取输入到 TextBox 中的字符串的值,以便我可以在整个应用程序中使用该字符串变量?

视图跟随页面顶部的继承来建模。此页面名为“InputNumbersSection”:

<%: Html.TextBoxFor(m => m.Number) %>

和行动:

<%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>

模型有这个:

 public class NumberModels
    {
        public string Number { get; set; }
    }

控制器有以下内容:

public ActionResult DisplayNumbersSection(NumberModels model)
        {

            if (ModelState.IsValid)
            {
                string TextBoxValue = model.Number;
                ViewData["Number"] = TextBoxValue;
            }      
            return View();
        }

我在另一个页面中使用的 ViewData 从视图中键入的文本框中返回数字。

当我在文本框中输入一些东西时,我没有看到该属性被命中或执行。 “数字”属性始终返回 NULL。似乎它没有接收到我在 TextBox 中输入的内容

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-2


    【解决方案1】:

    首先你应该有一个视图模型:

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

    将创建初始视图的控制器:

    public ActionResult YourEvent()
    {
        return View(new YourModel());
    }
    

    要创建强类型视图,您需要将以下内容添加到 Page 指令:

    <%@ Page Title="" Inherits="System.Web.Mvc.ViewPage<YourModel>" %>
    

    那么在你看来你可以这样做:

    <%: Html.TextBoxFor(m => m.YourProperty) %>
    

    然后在您发布表单时在您的控制器中:

    [HttpPost]
    public ActionResult YourEvent(YourModel model)
    {
        if (ModelState.IsValid)
        {
             string TextBoxValue = model.YourProperty;
             // do what you want with it
        }            
        // do something
    }
    

    强类型视图是最好的方法,但如果你想快速而肮脏地做到这一点,你可以随时访问Request 对象并检查Form

    编辑:添加了强类型视图定义。

    【讨论】:

    • 记得在页眉中添加 Inherits="System.Web.Mvc.ViewPage。否则这是最好的解决方案。
    • @Xenph Yan 谢谢,正如您所建议的,我添加了定义,使其成为更完整的答案。
    【解决方案2】:

    你可以从

    Request.Form
    

    但这是非常原始的解决方案。

    您必须使用强类型视图并在操作中使用您的模型。

    【讨论】:

      【解决方案3】:

      如果您需要 TextBox1 变量服务器端,则文本框必须在表单中发布到控制器中的操作,这意味着 TextBox1 变量将可用作表单发布到的操作的参数。 IE 在您看来,您可以:

      <% Using Html.BeginForm() %>
         <%: Html.TextBox("FirstName") %>
         <Input type=submit />
      <% End Using %>
      

      然后在控制器中你就会有方法

      Function Home(ByVal FirstName As String) As ActionResult
        MsgBox(FirstName)
      End Function
      

      【讨论】:

        【解决方案4】:

        与发布的 vb 解决方案相同,但在 c# 中。

        [HttpPost]
        public ActionResult YourActionMethod(string TextBox1)
        {
            //use value of TextBox1
        }
        

        【讨论】:

        • 哈哈是的,我想VB现在是例外而不是规则,我目前正在改进我的C#。 :P
        【解决方案5】:

        视图跟随页面顶部的继承来建模。此页面名为“InputNumbersSection”:

        <%: Html.TextBoxFor(m => m.Number) %>
        

        和行动:

        <%: Html.ActionLink("Get Number!", "DisplayNumbersSection") %>
        

        模型有这个:

         public class NumberModels
            {
                public string Number { get; set; }
            }
        

        控制器有以下内容:

        public ActionResult DisplayNumbersSection(NumberModels model)
                {
        
                    if (ModelState.IsValid)
                    {
                        string TextBoxValue = model.Number;
                        ViewData["Number"] = TextBoxValue;
                    }      
                    return View();
                }
        

        我在另一个页面中使用的 ViewData 从视图中键入的文本框中返回数字。

        当我在文本框中输入一些东西时,我没有看到该属性被命中或执行。 “数字”属性始终返回 NULL。似乎它没有接收到我在 TextBox 中输入的内容

        【讨论】:

        • ActionLink 不发布数据。它所做的只是建立一个链接。您需要将数据保存在 form 中并使用 input 将数据发布到您的控制器。另外作为旁注,您应该编辑原始问题并添加到其中。本部分仅供解答。您可以根据需要进行编辑,因此您应该继续添加/编辑,直到获得所需的答案。完成后不要忘记做出接受的答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-03
        • 2022-08-05
        • 1970-01-01
        • 2010-11-07
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多