我尝试了几种不同的方法是将数据从视图移动到控制器以便对新视图进行排序,但我无法获取数据。这就是我所拥有的:
查看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>
}
我觉得问题的大部分与我的Controller措辞有关。我尝试过使用请求名称来表示字符串,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; }
}
}
答案 0 :(得分:0)
public ActionResult Names(FormCollection form)
{
var letter = form["selectletter"];
ViewBag.UserID = new SelectList(db.users, "UserID", "FullName", null);
return View();
}
对我有用!