这是模型投票模型
public virtual int EmployeeId { get; set; }
public virtual int ElectionId { get; set; }
public virtual string ElectionName { get; set; }
public virtual DateTime EEnd { get; set; }
public virtual string CandidateName { get; set; }
public virtual int Estatus { get; set; }
public string EPosition { get; set; }
public virtual string EDepartment { get; set; }
public virtual string EDescription { get; set; }
我想在表格中显示选举ID,名称,estatus,部门,位置,来自所有行表draftdetails当electid = 55和 看看有多少评论我试过很多方法,但我无法得到它
在控制器模型中我有
public ActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(votingModel chk)
{
var id = chk.ElectionId;
string ids = Convert.ToString(id);
if (db.votingModels.Find(id) == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//votingModel votingModel = db.votingModels.Find(id);
if (id == null)
{
return HttpNotFound();
}
//var result = db.votingModels.Include("ElectionName").Where(p => p.ElectionId)
//db.votingModels = db.votingModels.Contains(id)
//model.Customers = db.Customers.OrderBy(
// m => m.CustomerID).Take(5).ToList();
var query = from a in db.votingModel
where SqlFunctions.StringConvert((double)a.ElectionId).Contains(id)
select a;
var item = query.FirstOrDefault();
if (item != null)
{
return View(item);
}
else
{
return View("NotFound"); //Let's show a view about item not found
}
return View(votingModel);
}
在视图模型中我有这个代码
@model election.Models.votingModel
@{
ViewBag.Title = "index";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
@Html.Label("election ID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.ElectionId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ElectionId, "", new { @class = "text-danger" })
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="submit" class="btn btn-default" />
</div>
</div>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.ElectionId)
</dt>
<dd>
@Html.DisplayFor(model => model.ElectionId)
</dd>
<dt>
@Html.DisplayNameFor(model => model.ElectionName)
</dt>
<dd>
@Html.DisplayFor(model => model.ElectionName)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EStart)
</dt>
<dd>
@Html.DisplayFor(model => model.EStart)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Estatus)
</dt>
<dd>
@Html.DisplayFor(model => model.Estatus)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EPosition)
</dt>
<dd>
@Html.DisplayFor(model => model.EPosition)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EDepartment)
</dt>
<dd>
@Html.DisplayFor(model => model.EDepartment)
</dd>
<dt>
@Html.DisplayNameFor(model => model.EDescription)
</dt>
<dd>
@Html.DisplayFor(model => model.EDescription)
</dd>
</dl>
}
第二个问题是,如果我想检查三个条件
我想检查一下员工是否已经投票 如果用户输入的选举数据存在于数据库中且且在同一行中存在,并且存在并且存在,则= 2表示他已经投票如何制作该条件。 谢谢所有
答案 0 :(得分:0)
Find
方法使用主键查找实体。由于ElectionId
中的VotingModel
是外键,因此您必须使用Where
,Single
或First
子句进行搜索。
以下代码假定votingModels表中只有一行具有给定的ElectionId
。如果有多个匹配的行,则必须更改此逻辑以使用Where
而不是Single
,并且您的视图必须采用@model election.Models.votingModel[]
。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(votingModel chk) {
// chk.ElectionId can never be null because this property is not nullable in VotingModel
long id = chk.ElectionId;
// assuming there is only a single entry with this ElectionId in the DB
votingModel existing = db.votingModels.SingleOrDefault(v => v.ElectionId == id);
// SingleOrDefault returns null if no entry matches the condition
if (existing == null) {
return View("NotFound");
}
return View(existing);
}
答案 1 :(得分:0)
你不能在相同的视图中同时接受输入和从数据库中检索,你可以对视图做什么从用户获取选举id将其传递给控制器如果条件然后显示另一个视图,请确保使用参数发送用户输入并进行验证 希望能回答你的问题