如何解决视图中的动态标签显示从另一个模型查询的信息
我试图让我的视图包含在页面上的其他输入字段更新时自动更新的标签。
当页面的名称和日期字段更新后,我想要一个标签来显示来自另一个模型的数据。可以通过使用用户刚刚输入的当前名称和日期查询数据库来获取数据。
当输入的日期或名称发生更改时,标签应自行更新。我该怎么做?
我的观点
@model Interchangesys.Models.Record
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Interchange Record</h4>
<hr />
<dt>
@Html.ValidationSummary(true,"",new { @class = "text-danger" })
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<br />
<div class="form-group">
  Requester Name <p>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#FullName").autocomplete({
source: function (request,response) {
$.ajax({
url: "/Record/Search",type: "POST",dataType: "json",data: { Keyword: request.term },success: function (data) {
response($.map(data,function (item) {
return { label: item.FullName,value: item.FullName };
}))
}
})
},messages: {
noresults: "",results: function (count) {
return count + (count > 1 ? ' results' : ' result ') + ' found';
}
}
});
})
</script>
@if (this.User.IsInRole("Supervisor"))
{
<div class="col-md-10">
@Html.TextBox("AltFullName",null,new { id = "AltFullName",@class = "form-control"})
</div>
}
else
{
<div class="col-md-10">
@Html.TextBox("AltFullName",@class = "form-control",@readonly = "readonly",@placeholder = User.Identity.Name })
</div>
}
<!--Name is used to get the EmployeeID in the controller later-->
</p>
</div>
<div class="form-group">
  Original Shift Date <p>
<div class="col-md-10">
@Html.EditorFor(model => model.BeforeDate,new { htmlAttributes = new { @class = "form-control",id = "beforeDate" } })
@Html.ValidationMessageFor(model => model.BeforeDate,new { @class = "text-danger" })
<!--Add code here to show a shift record's starttime endtime with matching Shiftdate and EmployeeID-->
</div>
</p>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<div class="form-group">
  Requestee Name <p>
<div class="col-md-10">
@Html.TextBox("FullName",new { id = "FullName",@class = "form-control" })
@Html.ValidationMessageFor(model => model.EmpBID,new { @class = "text-danger" })
<!--Name is used to get the EmployeeID in the controller later-->
</div>
</p>
</div>
<div class="form-group">
  Date of Shift for Interchange <p>
<div class="col-md-10">
@Html.EditorFor(model => model.AfterDate,new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AfterDate,new { @class = "text-danger" })
<!--Add code here to show a shift record's starttime endtime with matching Shiftdate and EmployeeID-->
</div>
</p>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</dt>
</div>
}
此视图的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using Interchangesys.Models;
namespace Interchangesys.Models
{
public enum Status
{
Pending,Accepted,Denied,Approved,Deleted
}
public class Record
{
[Key]
public int RecordID { get; set; }
[DataType(DataType.Date)]
[displayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
public DateTime? BeforeDate { get; set; }
[DataType(DataType.Date)]
[displayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
public DateTime? AfterDate { get; set; }
public int? ShiftIDBefore { get; set; }
public int? ShiftIDAfter { get; set; }
public int? EmpAID { get; set; }
public int? EmpBID { get; set; }
public Status Status { get; set; }
public virtual Employee EmployeeA { get; set; }
public virtual Employee EmployeeB { get; set; }
public virtual Shift ShiftBefore { get; set; }
public virtual Shift ShiftAfter { get; set; }
public virtual ICollection<History> Historys { get; set; }
public Record()
{
this.CreatedTime = DateTime.Now;
}
}
}
另一种存储“移位”数据的模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Interchangesys.Models
{
public class Shift
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ShiftID { get; set; }
public int EmployeeID { get; set; }
public DateTime Shiftdate { get; set; }
public string Starttime { get; set; }
public string Endtime { get; set; }
[InverseProperty("ShiftBefore")]
public virtual ICollection<Record> SendShifts { get; set; }
[InverseProperty("ShiftAfter")]
public virtual ICollection<Record> ReceiveShifts { get; set; }
public virtual Employee Employee { get; set; }
}
}
我的控制器的代码片段
namespace Interchangesys.Controllers
{
public class RecordController : Controller
{
private EmpContext db = new EmpContext();
// GET: Record/Create
public ActionResult Create()
{
return View();
}
// POST: Record/Create
[ValidateAntiForgeryToken]
public ActionResult Create(string Prefix,[Bind(Include = "RecordID,BeforeDate,AfterDate,ShiftIDBefore,ShiftIDAfter,EmpAID,EmpBID,Reason,Self_interchange,Status,Transfer")] Record record,string FullName,string AltFullName)
{
if (ModelState.IsValid)
{
record.Status = Status.Pending;
if (String.IsNullOrEmpty(AltFullName))
{
var EA = (from s in db.Employees
where ((s.Login_Name == User.Identity.Name) && (s.EStatus == EStatus.Active))
select s).SingleOrDefault();
record.EmpAID = EA.ID;
} else
{
var EA = (from s in db.Employees
where ((s.FullName == AltFullName) && (s.EStatus == EStatus.Active))
select s).SingleOrDefault();
record.EmpAID = EA.ID;
}
if (String.IsNullOrEmpty(FullName))
{
var EB = (from s in db.Employees
where ((s.Login_Name == User.Identity.Name) && (s.EStatus == EStatus.Active))
select s).SingleOrDefault();
record.EmpBID = EB.ID;
}
else
{
var EB = (from s in db.Employees
where ((s.FullName.Equals(FullName)) && (s.EStatus == EStatus.Active))
select s).SingleOrDefault();
record.EmpBID = EB.ID;
}
var SA = (from s in db.Shifts
join i in db.Employees on s.EmployeeID equals i.ID
where (s.EmployeeID == record.EmpAID && s.Shiftdate == record.BeforeDate)
select s).SingleOrDefault();
record.ShiftIDBefore = SA.ShiftID;
var SB = (from s in db.Shifts
join i in db.Employees on s.EmployeeID equals i.ID
where (s.EmployeeID == record.EmpBID && s.Shiftdate == record.AfterDate)
select s).SingleOrDefault();
record.ShiftIDAfter = SB.ShiftID;
if (record.Self_interchange == true)
{
record.Status = Status.Accepted;
}
record.Transfer = false;
db.Records.Add(record);
db.SaveChanges();
Record record2 = new Record();
record2.BeforeDate = record.AfterDate;
record2.AfterDate = record.BeforeDate;
var SA2 = (from s in db.Shifts
join i in db.Employees on s.EmployeeID equals i.ID
where (s.EmployeeID == record.EmpAID && s.Shiftdate == record.AfterDate)
select s).SingleOrDefault();
record2.ShiftIDBefore = SA2.ShiftID;
var SB2 = (from s in db.Shifts
join i in db.Employees on s.EmployeeID equals i.ID
where (s.EmployeeID == record.EmpBID && s.Shiftdate == record.BeforeDate)
select s).SingleOrDefault();
record2.ShiftIDAfter = SB2.ShiftID;
record2.EmpAID = record.EmpAID;
record2.EmpBID = record.EmpBID;
record2.Self_interchange = record.Self_interchange;
record2.Status = record.Status;
record2.Transfer = false;
db.Records.Add(record2);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(record);
}
[HttpPost]
[ValidateAntiForgeryToken]
public JsonResult Search(string Keyword)
{
var empquery = (from c in db.Employees
where c.FullName.Contains(Keyword)
select new { c.FullName,c.ID });
return Json(empquery,JsonRequestBehavior.AllowGet);
}
}
}
我想用 jQuery Ajax 做过滤,我应该如何编写控制器和视图?提前致谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。