Asp.net mvc 3 MVC nerd晚餐CreateView未显示事件日期
我目前正在使用MVS 2010进行MVC教程“书呆子晚餐”,我已经完成了第7步,但我刚刚注意到,当我现在进入“创建”屏幕时,它并不完全正确Asp.net mvc 3 MVC nerd晚餐CreateView未显示事件日期,asp.net-mvc-3,nerddinner,Asp.net Mvc 3,Nerddinner,我目前正在使用MVS 2010进行MVC教程“书呆子晚餐”,我已经完成了第7步,但我刚刚注意到,当我现在进入“创建”屏幕时,它并不完全正确 标题输入框似乎包含ViewBag.title,而不是空白 EventDate输入框为空,此时应从现在起自动设置为7天 我不记得之前在教程中是这样的 下面是来自dinerscontroller.cs的snippit,用于处理创建: // // GET: /Dinners/Create public ActionResult Creat
ViewBag.title
,而不是空白EventDate
输入框为空,此时应从现在起自动设置为7天dinerscontroller.cs
的snippit,用于处理创建:
//
// GET: /Dinners/Create
public ActionResult Create()
{
Dinner dinner = new Dinner()
{
EventDate = DateTime.Now.AddDays(7)
};
return View(new DinnerFormViewModel(dinner));
}
//
// POST: /Dinners/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinner dinner)
{
if (ModelState.IsValid)
{
try
{
dinner.HostedBy = "SomeUser";
dinnerRepository.Add(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
ModelState.AddModelErrors(dinner.GetRuleViolations());
}
}
return View(new DinnerFormViewModel(dinner));
}
下面是视图Create.cshtml
@model NerdDinner.Models.DinnerFormViewModel
@{
ViewBag.Title = "Host a Dinner";
}
<h2>Host a Dinner</h2>
@Html.ValidationSummary("Please correct the errors and try again")
@using (Html.BeginForm()) {
<fieldset>
<p>
@Html.LabelFor(model => Model.Dinner.Title)
<br />
@Html.TextBox("Title")
@Html.ValidationMessage("Title", "*")
</p>
<p>
@Html.LabelFor(model => Model.Dinner.EventDate)
<br />
@Html.TextBox("EventDate")
@Html.ValidationMessage("EventDate", "*")
</p>
<p>
@Html.LabelFor(model => Model.Dinner.Description)
<br />
@Html.TextArea("Description")
@Html.ValidationMessage("Description", "*")
</p>
<p>
@Html.LabelFor(model => Model.Dinner.Address)
<br />
@Html.TextBox("Address")
@Html.ValidationMessage("Address", "*")
</p>
<p>
@Html.LabelFor(model => Model.Countries)
<br />
@Html.DropDownList("Country", Model.Countries)
@Html.ValidationMessage("Country", "*")
</p>
<p>
@Html.LabelFor(model => Model.Dinner.ContactPhone)
<br />
@Html.TextBox("ContactPhone")
@Html.ValidationMessage("ContactPhone", "*")
</p>
<p>
@Html.LabelFor(model => Model.Dinner.Latitude)
<br />
@Html.TextBox("Latitude")
@Html.ValidationMessage("Latitude", "*")
</p>
<p>
<label for="Longitude">Longitude:</label>
<br />
@Html.TextBox("Longitude")
@Html.ValidationMessage("Longitude", "*")
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model NerdDinner.Models.viewmodel
@{
ViewBag.Title=“主持晚宴”;
}
设宴
@ValidationSummary(“请更正错误并重试”)
@使用(Html.BeginForm()){
@LabelFor(model=>model.Dinner.Title)
@Html.TextBox(“标题”)
@Html.ValidationMessage(“标题”,“*”)
@LabelFor(model=>model.Dinner.EventDate)
@Html.TextBox(“事件日期”)
@Html.ValidationMessage(“EventDate”,“*”)
@LabelFor(model=>model.Dinner.Description)
@Html.TextArea(“说明”)
@Html.ValidationMessage(“说明”、“*”)
@LabelFor(model=>model.Dinner.Address)
@文本框(“地址”)
@Html.ValidationMessage(“地址”、“*”)
@LabelFor(model=>model.Countries)
@Html.DropDownList(“国家”,Model.Countries)
@Html.ValidationMessage(“国家”,“*”)
@LabelFor(model=>model.Dinner.ContactPhone)
@Html.TextBox(“联系人电话”)
@Html.ValidationMessage(“ContactPhone”和“*”)
@LabelFor(model=>model.Dinner.Latitude)
@文本框(“纬度”)
@Html.ValidationMessage(“纬度”,“*”)
经度:
@文本框(“经度”)
@Html.ValidationMessage(“经度”、“*”)
}
@ActionLink(“返回列表”、“索引”)
最后是浏览器中的输出:
有人知道我错过了什么吗
编辑-添加晚餐模式
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text; //added this - not in tut
using System.Text.RegularExpressions; //added this - not in tut
namespace NerdDinner.Models
{
[Bind(Include = "Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude")]
public partial class Dinner
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required", "Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
{
yield return new RuleViolation("ContactPhone required", "ContactPhone");
}
else
{
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "ContactPhone");
}
yield break;
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule voilations prevent saving");
}
}
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
public class PhoneValidator
{
static IDictionary<string, Regex> countryRegex = new Dictionary<string, Regex>()
{
{ "USA", new Regex("^[2-9]\\d{2}-\\d{3}-\\d{4}$")},
{ "UK", new Regex("(^1300\\d{6}$)|(^1800|1900|1902\\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^04\\decimal{2,3}\\decimal{6}$)")},
{ "Netherlands", new Regex("(^\\+[0-9]{2}|^\\+[0-9]{2}\\(0\\)|^\\(\\+[0-9]{2}\\)\\(0\\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\\-\\s]{10}$)")},
};
public static bool IsValidNumber(string phoneNumber, string country)
{
if (country != null && countryRegex.ContainsKey(country))
return countryRegex[country].IsMatch(phoneNumber);
else
return false;
}
public static IEnumerable<string> Countries
{
get
{
return countryRegex.Keys;
}
}
}
public class DinnerFormViewModel
{
// Properties
public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }
// Contructor
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
Countries = new SelectList(PhoneValidator.Countries, dinner.Country);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Linq;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用系统文本//增加了这个-不是在图坦卡蒙
使用System.Text.RegularExpressions//增加了这个-不是在图坦卡蒙
名称空间NerdDinner.Models
{
[Bind(Include=“Title,Description,EventDate,Address,Country,ContactPhone,Latitude,Longitude”)]
公共半课堂晚餐
{
公共布尔是有效的
{
获取{return(GetRuleViolations().Count()==0);}
}
公共IEnumerable GetRuleViolations()
{
if(String.IsNullOrEmpty(Title))
退回新的违规行为(“需要标题”、“标题”);
if(String.IsNullOrEmpty(Description))
退回新的违规行为(“需要说明”、“说明”);
if(String.IsNullOrEmpty(HostedBy))
收益返回新规则违规(“要求托管比”、“托管比”);
if(String.IsNullOrEmpty(Address))
退回新的违规行为(“所需地址”、“地址”);
if(String.IsNullOrEmpty(国家))
收益返回新规则违反(“国家要求”、“国家”);
if(String.IsNullOrEmpty(ContactPhone))
{
返回新的违规行为(“需要联系电话”、“联系电话”);
}
其他的
{
如果(!PhoneValidator.IsValidNumber(联系人电话,国家/地区))
返回新规则违规(“电话与国家/地区不匹配”、“联系电话”);
}
屈服断裂;
}
验证时部分作废(更改操作)
{
如果(!IsValid)
抛出新的ApplicationException(“规则查看阻止保存”);
}
}
违反公共类规则
{
公共字符串错误消息{get;private set;}
公共字符串PropertyName{get;private set;}
公共规则冲突(字符串错误消息、字符串属性名称)
{
ErrorMessage=ErrorMessage;
PropertyName=PropertyName;
}
}
公共类电话验证器
{
静态IDictionary countryRegex=新字典()
{
{“USA”,新的正则表达式(“^[2-9]\\d{2}-\\d{3}-\\d{4}$”),
{“UK”,新的正则表达式(^1300{6}$)|(^1800{1900}1902{6}$)|(^0[2{3}7{8]{1}[0-9]{8}$)|(^04{2,3}\\decimal 6}$),
{“荷兰”,新的正则表达式(“(^\+[0-9]{2}\\\\\\\\\\\\+[0-9]{2}\\\\\\\\\(\\\+[0-9]{2}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\[0-9]{2}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
};
公共静态bool IsValidNumber(字符串电话号码,字符串国家/地区)
{
if(country!=null&&countryRegex.ContainsKey(country))
返回countryRegex[country].IsMatch(电话号码);
其他的
返回false;
}
无数国家的公共安全
{
得到
{
返回countryRegex.key;
}
}
}
公共类视图模型
{
//性质
公共晚餐{获得;私人套餐;}
公共选择列表国家{get;private set;}
//承包商
公共晚餐模式(晚餐)
{
<p>
@Html.LabelFor(model => Model.Dinner.EventDate)
<br />
@Html.TextBoxFor(model => Model.Dinner.EventDate)
@Html.ValidationMessageFor(model => Model.Dinner.EventDate)
</p>
public class DinnerViewModel{
[DisplayName("Dinner Location")]
[Required(ErrorMessage="You must specify a location")]
public string Location {get;set;}
}
<p>
@Html.LabelFor(model => Model.Location )
<br />
@Html.TextBoxFor(model => Model.Location)
@Html.ValidationMessageFor(model => Model.Location )
</p>
<p>
<label for="Location">Dinner Location</label>
<br/>
<input type="text" name="Location" id="Location"/>
*the validation related stuff*
</p>
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Dinner.Title");
if (String.IsNullOrEmpty(Description))
yield return new RuleViolation("Description required", "Dinner.Description");
if (String.IsNullOrEmpty(HostedBy))
yield return new RuleViolation("HostedBy required", "Dinner.HostedBy");
if (String.IsNullOrEmpty(Address))
yield return new RuleViolation("Address required", "Dinner.Address");
if (String.IsNullOrEmpty(Country))
yield return new RuleViolation("Country required", "Country");
if (String.IsNullOrEmpty(ContactPhone))
{
yield return new RuleViolation("ContactPhone required", "Dinner.ContactPhone");
}
else
{
if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
yield return new RuleViolation("Phone# does not match country", "Dinner.ContactPhone");
}
yield break;
}