如何将数据从视图上的文本框传递到控制器中的操作方法? [英] How do you pass data from a textbox on a view to an action method in the controller?

查看:156
本文介绍了如何将数据从视图上的文本框传递到控制器中的操作方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下观点:

I have the following view:

@model ReportViewer.Models.ViewModel.EntryViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        @using (Html.BeginForm("UserReportView", "HomeController", FormMethod.Post))
        {
            <input id="UserToLookUp" type="text" width="50" name="UserToLookUp" />
            @Html.TextBoxFor(model => model.UserToLookUp)
            <input type = "submit" value = "View" />
        }
    </div>
</body>
</html>





它使用以下型号:



It uses the following model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ReportViewer.Models.ViewModel
{
    public class EntryViewModel
    {
        public string CurrentUser { get; set; }
        public string UserToLookUp { get; set; }
    }
}





我的控制器如下:



My controller is as follows:

using ReportViewer.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ReportViewer.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            EntryViewModel vm = new EntryViewModel();
            return View(vm);
        }

        //[HttpPost]
        public ActionResult UserReportView(EntryViewModel vm)
        {
            TempData["ID"] = vm.UserToLookUp;

            return RedirectToAction("Index", "Viewer");
        }
    }
}





我需要从UserToLookUp文本框中获取值在ActionResult UserReportView的TempData [ID]中。 vm在方法中是可访问的,但UserToLookUp始终为null。




我尝试过:



过去两天我花了大部分时间用谷歌搜索并尝试在各种教程中看到的所有事情的排列,并且无法让它发挥作用。



I need to get the value from the "UserToLookUp" textbox in to the TempData["ID"] of the ActionResult UserReportView. The vm is accessable in the method but UserToLookUp is always null.


What I have tried:

I've spent the better part of the last two days googling and trying every permutation of things I've seen in various tutorials and can not get it to work.

推荐答案

更正

Corrections
@using (Html.BeginForm("UserReportView", "HomeController", FormMethod.Post))




<input id="UserToLookUp" type="text" width="50" name="UserToLookUp" />


这篇关于如何将数据从视图上的文本框传递到控制器中的操作方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆