MVC:将值从文本框传递到控制器操作

时间:2016-03-14 08:17:52

标签: asp.net-mvc-4

我是MVC的新手。基本上我需要将文本框中输入的值从我的视图传递给控制器​​操作方法。当我在文本框中输入值并单击输入按钮时,我需要在屏幕上显示值。我目前无法这样做。请在下面找到我的代码 模型类

 public class ProteinTrackingService
    {
        public int? Total { get; set; }
        public int Goal { get; set; }

        public void AddProtein(int? amount)
        {
            Total += amount;
        }

    }

控制器类

 public class ProteinTrackerController : Controller
    {

        ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
        // GET: ProteinTracker
        public ActionResult Index()
        {
            ViewBag.Total = proteinTrackingService.Total;
            ViewBag.Goal = proteinTrackingService.Goal;
            return View();
        }

        // GET: ProteinTracker/Details/5

        public ActionResult AddProtein(ProteinTrackingService model)
        {
            proteinTrackingService.AddProtein(model.Total);
            ViewBag.Total = proteinTrackingService.Total;
            ViewBag.Goal = proteinTrackingService.Goal;

            return View("Index");
        }


    }

视图

using (Html.BeginForm("ProteinTracker", "AddProtein",FormMethod.Post))
{
    @Html.AntiForgeryToken()



        <form>
            <div class="form-horizontal">
                <h4>Protein Tracker</h4>
                <hr />

                Total : @ViewBag.Total
                Goal  : @ViewBag.Goal

                <input id="Text1" type="text" value="TextInput" />  <input type="Submit" value="Add"  />



            </div>
        </form>

}

我正在根据您的建议修改上面的代码。我基本上需要在视图中显示以下内容

总计:价值 目标:价值

文本框控件(输入总数)按钮(将总计传递给控制器​​)​​请注意,当用户单击“添加”按钮时,总计应显示在上面的字段“总计:值”中。

新视图

@using (Html.BeginForm( "AddProtein","ProteinTracker", FormMethod.Post))
{
    @Html.AntiForgeryToken()

            <div class="form-horizontal">
                <h4>Protein Tracker</h4>
                <hr />

                 @Html.LabelFor(m => m.Total, "Total" )  <hr />
                 @Html.LabelFor(m => m.Goal, "Goal")   <hr />
                 @Html.TextBoxFor(m => m.Total)   <hr />

                 <input type="Submit" value="Add"  />
            </div>

}

新控制器

public class ProteinTrackerController:Controller {

ProteinTrackingService proteinTrackingService = new ProteinTrackingService();
// GET: ProteinTracker
public ActionResult Index()
{
    var model = new ProteinTrackingService()
    { Total = proteinTrackingService.Total, Goal = proteinTrackingService.Goal };
    return View(model);

}

// GET: ProteinTracker/Details/5

public ActionResult AddProtein(ProteinTrackingService model)
{
    proteinTrackingService.AddProtein(model.Total);
    model.Total = proteinTrackingService.Total;
    model.Goal = proteinTrackingService.Goal;

    return View("Index",model);
}

}

2 个答案:

答案 0 :(得分:0)

您需要在操作中添加HttpPost属性。查看表单@using (Html.BeginForm( "AddProtein","ProteinTracker", FormMethod.Post)),显然您正在向控制器发送post请求。

[HttpPost]
public ActionResult AddProtein(ProteinTrackingService model)
{
    proteinTrackingService.AddProtein(model.Total);
    model.Total = proteinTrackingService.Total;
    model.Goal = proteinTrackingService.Goal;

    return View("Index",model);
}

答案 1 :(得分:-1)

首先你的这个语法

using (Html.BeginForm("ProteinTracker", "AddProtein", FormMethod.Post))

在html生成时已创建表单标记。无需再次在标签中创建。

因此,根据您的需要,您需要在输入字段中输入名称

 <input id="Text1" type="text" value="TextInput" name="textname"/>

并在控制器方法中添加此名称作为参数

public ActionResult AddProtein(ProteinTrackingService model,string textname)
{
    // your code
    return View("Index");
}

它会将您的textfield值从视图传递给控制器​​。要清除您的概念,您可以访问Loopcoder.com