我有一个视图有两个部分视图,一个用于创建,另一个是列出项目我使用视图模型作为主视图的模型
我的观点如下
@model Project.Web.Areas.Administrator.Models.VFranchise
<!-- content starts -->
<div class=" large-10 medium-10 small-10 columns band">
<div class="row no-margin">
<div class="content-head ">
<!--breadcrumbs-->
<ul>
<li>@Html.ActionLink("Home", "GetAll")</li>
<li>></li>
<li>@Html.ActionLink("Franchise", "GetAll")</li>
<li>></li>
<li><a href="">Create New Franchise</a></li>
<li>></li>
</ul>
</div>
@{Html.RenderAction("CreateFranchise", "Franchise", Model.Franchise);}
@{Html.RenderAction("ListAllFranchise", "Franchise", Model.Franchisees);}
</div>
</div>
我的视图模型如下
public class VFranchise
{
public Franchaise Franchise { get; set; }
public IEnumerable<Franchaise> Franchisees { get; set; }
}
我要创建的部分视图如下
@model Project.BAL.Models.Master.Franchaise
<div class="general-wrapper">
<!--content box-->
<div class="general-head">
<h2 class="no-margin">Create New Franchise</h2>
<div class="general-icons-org">
<a href=""><i class="fa fa-minus"></i></a>
<a href=""><i class="fa fa-square-o"></i></a>
<a href=""><i class="fa fa-times"></i></a>
</div>
</div>
@using (Html.BeginForm("Create", "Franchise", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="minus-wrapper">
<div class="org-general-wrapper">
<div class="org-wrapper">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<ul class="small-block-grid-1 medium-block-grid-2 large-block-grid-2">
<li>
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.ExpiryDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.ExpiryDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ExpiryDate, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
</div>
</li>
<li>
@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="inner">
@Html.DropDownListFor(m => m.Status, new List<SelectListItem>{
new SelectListItem{ Text="Active", Value = "1" },
new SelectListItem{ Text="Deactive", Value = "2" },
}, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" })
</div>
</li>
<li>
<label> </label>
<div class="inner">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</li>
</ul>
<div class="req-wrapper">
<p>* required field</p>
</div>
</div>
</div>
</div>
}
<div class="edit-wrapper">
@Html.ActionLink("Back to List", "GetAll")
</div>
</div>
这是我的主视图控制器
public ActionResult Create()
{
return View(new VFranchise());
}
这是我的Post方法
[HttpPost]
public ActionResult Create(Franchaise model)
{
if (ModelState.IsValid)
{
model.FranchaiseId = Guid.NewGuid();
model.Status = Convert.ToInt32(model.Status);
model.StartDate = DateTime.Now;
_FranchaiseServices.Create(model);
return RedirectToAction("GetAll");
}
else {
var _model = new VFranchise();
_model.Franchise = model;
return View(_model);
}
}
以下是我的部分视图控件
public PartialViewResult CreateFranchise(Franchaise model)
{
return PartialView(model);
}
public PartialViewResult _ListFranchiseByExpiry(string id)
{
int _id = Convert.ToInt32(id);
var items = _FranchaiseServices.GetAllFranchiseByExpiry(_id);
return PartialView(items);
}
问题是我在创建自己的HTTP get调用时遇到验证错误。
如果我这样做
public PartialViewResult CreateFranchise()
{
return PartialView(new Franchaise());
}
我也没有在帖子上收到任何验证错误,有人可以告诉我,我在这里做错了。谢谢!
答案 0 :(得分:2)
您在// works for 3.6 using internal classes, most probably will not work for other versions
public static void main(String[] args) {
// start a hazelcast instance
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
// create a CacheManager and Cache on this instance
CachingProvider hazelcastCachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider",
HazelcastCachingProvider.class.getClassLoader());
CacheManager cacheManager = hazelcastCachingProvider.getCacheManager();
cacheManager.createCache("test1", new CacheConfig<Object, Object>());
// hacky: obtain a reference to internal cache service
CacheDistributedObject cacheDistributedObject = hz.getDistributedObject("hz:impl:cacheService", "setupRef");
ICacheService cacheService = cacheDistributedObject.getService();
// obtain all CacheConfig's in the cluster
Collection<CacheConfig> cacheConfigs = cacheService.getCacheConfigs();
for (CacheConfig cacheConfig : cacheConfigs) {
System.out.println("Cache name: " + cacheConfig.getName() +
", fully qualified name: " + cacheConfig.getNameWithPrefix());
}
hz.shutdown();
}
方法上获得验证错误的原因是因为传递给方法的模型包含验证属性且其值无效(例如,{{1}上可能有CreateFranchise(Franchaise model)
属性属性,但是[Required]
DefaultModelBinder string
ModelState`错误的值,然后显示在视图中。
视图模型不应包含属于数据模型的属性,至少用于编辑),它应该是
null), so the
和视图的GET方法
adds
并在主视图中
public class FranchaiseVM
{
[Required(ErrorMessage = "Please enter a name")]
public string Name { get; set; }
.... // copy other properties of Franchaise that you want to edit
public int Status { get; set; }
public IEnumerable<SelectListItem> StatusList { get; set; }
public IEnumerable<Franchaise> Franchaises { get; set; }
}
和POST方法
FranchaiseVM model = new FranchaiseVM()
{
StatusList = new List<SelectListItem>()
{
new SelectListItem{ Text="Active", Value = "1" },
new SelectListItem{ Text="Deactive", Value = "2" }
},
Franchaises = .... // your query for populating the collection
};
return View(model)
附注:在这种情况下,似乎没有理由使用@model FranchaiseVM
....
@using (Html.BeginForm("Create", "Franchise", FormMethod.Post))
{
.... your form controls
}
@{ Html.RenderAction("ListAllFranchise", "Franchise", Model.Franchisees); }
生成现有项目。更好的方法是在[HttpPost]
public ActionResult Create(FranchaiseVM viewModel)
{
if (!ModelState.IsValid)
{
model.StatusList = ... as per GET method
model.Franchaises = ... as per GET method
return View(viewModel);
}
// Initialize data model and set its properties
Franchaise dataModel = new Franchaise()
{
Name = viewModel.Name,
.... // map other properties
Status = viewModel.Status, // not sure why you need to convert it
StartDate = DateTime.Now;
}
_FranchaiseServices.Create(dataModel);
return RedirectToAction("GetAll");
}
RenderAction()
DisplayTemplate
并在主视图中使用/Views/Shared/DisplayTemplates/Franchaise.cshtml
,它将为集合中的每个项目生成html。