将数据视图传递给控制器

本文关键字:控制器 数据 视图 | 更新日期: 2023-09-27 18:08:18

我尝试了几种不同的方法。为了对新视图进行排序,我试图将数据从视图移动到控制器,但我无法获得要传递的数据

查看1

@model TabCheckout.checkout
@{
    ViewBag.Title = "Select the Letter of Your Last Name";
}
<h3>Select the Letter of Your Last Name</h3>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-10">
                @{int i = 0;
                    foreach (string letter in ViewBag.Letters)
                    {
                        i++;
                        if (i == 9)
                        {
                            i = 0;
                        @Html.Raw("<br />")
                        }
                        else
                        {
                            <input type='submit' id='@letter' name='selectletter' value='@letter' formaction='Names' />                    
                            }
                        }
                    }
            </div>
        </div>
    </div>
}

控制器

public ActionResult Letters(string selectletter)
        {
            List<string> letters = new List<string>();
            for (int y = 0; y < 26; y++)
            { 
                char filter = Convert.ToChar(65 + y);
                string letter = filter.ToString();
                letters.Add(letter);
            }
            ViewBag.Letters = letters;
            GlobalVariable.selectletter = Convert.ToString(selectletter);
            return View(GlobalVariable.selectletter);
        }
        public ActionResult Names()
        {
            //     var namesrt = from s in db.users
            //                   select s;
            //     namesrt = namesrt.Where(s => s.LastName.StartsWith(GlobalVariable.letter));
           // ViewBag.UserID = new SelectList(namesrt, "UserID", "FullName", null);
            ViewBag.UserID = new SelectList(db.users, "UserID", "FullName", null);
            return View();
        }

视图2

@model TabCheckout.checkout
@{
    ViewBag.Title = "Select Your Name";
}
<h3>Select Your Name - @GlobalVariable.selectletter</h3>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <div class="col-md-10">
                @{int i = 0;
                    foreach (var item in ViewBag.UserID as SelectList)
                    {
                        i++;
                        if (i == 9)
                        {
                            i = 0;
                            @Html.Raw("<br />")
                        }
                        else
                        {
                            <input type="submit" name="@item.Text" value="@item.Text" formaction="Vehicles">
                        }
                    }
                }
            </div>
        </div>
    </div>
}

我觉得大部分问题都与我的总监措辞有关。我已经尝试过使用请求名称来字符串、FormCollection和当前的混乱。

感谢您的帮助和对我有限技能水平的理解。

以下是完整披露的模型:

型号

namespace TabCheckout
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
    [Table("NewsData_Tab.checkout")]
    public partial class checkout
    {
        public int CheckoutID { get; set; }
        [Required]
        public int User { get; set; }
        [ForeignKey("User")]
        public virtual user users { get; set; }
        [Required]
        public int Vehicle { get; set; }
        [ForeignKey("Vehicle")]
        public virtual vehicle vehicles { get; set; }
        [Required]
        public int Equipment { get; set; }
        public DateTime TimeOut { get; set; }
        public DateTime TimeIn { get; set; }
        public checkout()
        {
            TimeOut = DateTime.Now;
        }
    }
    public static class GlobalVariable
    {
        public static string selectletter { get; set; }
    }
}

将数据视图传递给控制器

    public ActionResult Names(FormCollection form)
    {
        var letter = form["selectletter"];
        ViewBag.UserID = new SelectList(db.users, "UserID", "FullName", null);
        return View();
    }

它对我有用!