首先,我自学MVC,所以这些都不是最佳实践,这个问题可能被认为是MVC 101.我用Google搜索并尝试了好几天,所以在挫折中我正在寻求帮助。
我正在使用MVC5,EF6和Bootstrap 3
我的目标很简单 - 我希望显示一个视图,让用户使用时钟式时间戳为Bootstrap The actual Clockpicker found here on GitHub输入时间。当用户在视图上单击“创建”时,其结果将填充回我的数据库。
但是,我似乎无法让它发挥作用。下面我已经包含了Model,Controller和View的简化版本。在视图中,我可以使用我在示例中找到的HTML来显示clockpicker,但我不确定如何绑定到我的模型。如果我使用html辅助方法使用我为其他bootstrap元素所做的方式,它将不会显示时钟选择器。如果我按照示例编写代码,它将显示clockpicker但我不知道如何将结果绑定回我的模型,因此也就是数据库。如果您有任何示例代码或者可以指向某个可以解释我应该如何做的地方,我将不胜感激。
我希望这个问题有道理。
模型
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ATAS.Models
{
public partial class Authorisation : IControllerHooks
{
public long AuthorisationId { get; set; }
[Required, StringLength(10)]
public string AuthorisationNumber { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime StartTime { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime FinishTime { get; set; }
}
}
CONTROLLER (我刚刚收到了创建动作)
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using ATAS.Data;
using ATAS.Models;
namespace ATAS.Controllers
{
public class AuthorisationsController : Controller
{
private DataManager db = new DataManager();
// GET: /Authorisations/Create
public ActionResult Create()
{
ViewBag.AuthorisedEmployeeId = new SelectList(db.Employees, "EmployeeId", "FullName");
ViewBag.PaymentTypeId = new SelectList(db.OvertimeTypes, "PaymentTypeId", "Description");
ViewBag.AuthorisingEmployeeId = new SelectList(db.Employees, "EmployeeId", "FullName");
return View();
}
// POST: /Authorisations/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="AuthorisationId,DateAuthorised,StartTime,FinishTime")] Authorisation authorisation)
{
if (ModelState.IsValid)
{
if (authorisation is IControllerHooks) { ((IControllerHooks)authorisation).OnCreate(); }
db.Authorisations.Add(authorisation);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AuthorisedEmployeeId = new SelectList(db.Employees, "EmployeeId", "FullName", authorisation.AuthorisedEmployeeId);
ViewBag.PaymentTypeId = new SelectList(db.OvertimeTypes, "PaymentTypeId", "Description", authorisation.PaymentTypeId);
ViewBag.AuthorisingEmployeeId = new SelectList(db.Employees, "EmployeeId", "FullName", authorisation.AuthorisingEmployeeId);
return View(authorisation);
}
查看(CREATE.cshtml)
@model ATAS.Models.Authorisation
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Authorisation</h4>
<hr />
@Html.ValidationSummary(true)
<div class="col-sm-12">
<div class="form-group">
<label>Clockpicker:</label>
<div class="input-group">
<input class="form-control" id="clockpicker" type="text" placeholder="Select time" data-autoclose="true">
<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinishTime, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.FinishTime, new { @class = "clockpicker" })<br />
@Html.ValidationMessageFor(model => model.FinishTime)
</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>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section pagespecific {
<script src="/scripts/plugin/clockpicker/clockpicker.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// PAGE RELATED SCRIPTS
/*
* CLOCKPICKER
*/
$('#clockpicker').clockpicker({
placement: 'top',
donetext: 'Done'
});
})
</script>
}
问题
*****这种方法显示了clockpicker,但我不知道如何绑定到模型*****
<div class="col-sm-12">
<div class="form-group">
<label>Clockpicker:</label>
<div class="input-group">
<input class="form-control" id="clockpicker" type="text" placeholder="Select time" data-autoclose="true">
<span class="input-group-addon"><i class="fa fa-clock-o"></i></span>
</div>
</div>
</div>
*****这种方法不会显示clockpicker *****
<div class="form-group">
@Html.LabelFor(model => model.FinishTime, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.FinishTime, new { @class = "clockpicker" })<br />
@Html.ValidationMessageFor(model => model.FinishTime)
</div>
</div>
答案 0 :(得分:0)
睡觉之后好吧......我已经解决了。
问题始终在于如何将脚本连接在一起。
@section pagespecific {
<script src="/scripts/plugin/clockpicker/clockpicker.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// PAGE RELATED SCRIPTS
/*
* CLOCKPICKER
*/
$('#clockpicker').clockpicker({
placement: 'top',
donetext: 'Done'
});
}) </script>
}
我使用id selector代替class selector(。{3}}(。时间戳)
显示有时只是描述问题可以创建所需的顿悟。