我有一個MVC 4應用程序,我有一個ViewModel,它有幾個集合。在POST期間,集合爲空,但其他字段已填充。附上代碼請幫忙。ViewModel在POST後部分爲空
視圖模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BAQueryTool.Models
{
public class QueryDefinition
{
[Key]
public int ID { get; set; }
[Display(Name = "Query Name")]
[MaxLength(32)]
[Required(ErrorMessage = "Query name is required.")]
public string Name { get; set; }
[Display(Name = "Query String")]
[Required(ErrorMessage = "Query string is required.")]
[DataType(DataType.MultilineText)]
[MaxLength(8192)]
public string QueryString { get; set; }
[NotMapped]
[Display(Name = "Query String")]
[MaxLength(175)]
public string QueryStringShort
{
get
{
string s = string.Empty;
if (this.QueryString != null)
{
s = this.QueryString.Trim();
if (s.Length >= 172)
{
return this.QueryString.Substring(0, 172) + "...";
}
}
return s;
}
}
[Display(Name = "Query Parameters")]
public virtual ICollection<QueryParameter> QueryParameters { get; set; }
}
public class RunQueryDefinition
{
public QueryDefinition QueryDefinition { get; set; }
[Display(Name = "Run Query Parameters")]
public virtual ICollection<RunQueryParameter> RunQueryParameters { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BAQueryTool.Models
{
public enum ParameterDataType
{
Date, String
}
public class QueryParameter
{
[Key]
public int ID { get; set; }
public int QueryDefinitionID { get; set; }
[Display(Name = "Parameter Name")]
[Required(ErrorMessage = "Parameter name is required.")]
[MaxLength(32)]
public string Name { get; set; }
[Display(Name = "Parameter Type")]
[Required(ErrorMessage = "Parameter name is required.")]
public ParameterDataType DataType { get; set; }
}
public class RunQueryParameter : QueryParameter
{
[Display(Name = "Parameter Value")]
[Required(ErrorMessage = "Parameter name is required.")]
public string Value { get; set; }
}
}
控制器
` `// GET: /QueryDefinition/Run/5
public ActionResult Run(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
QueryDefinition querydefinition = db.QueryDefinitions.Find(id);
if (querydefinition == null)
{
return HttpNotFound();
}
RunQueryDefinition runQueryDefinition = new RunQueryDefinition();
runQueryDefinition.QueryDefinition = querydefinition;
runQueryDefinition.RunQueryParameters = new List<RunQueryParameter> { };
foreach (var param in querydefinition.QueryParameters)
{
RunQueryParameter rqp = new RunQueryParameter();
rqp.ID = param.ID;
rqp.QueryDefinitionID = param.QueryDefinitionID;
rqp.Name = param.Name;
rqp.DataType = param.DataType;
rqp.Value = Convert.ToString(DateTime.Now.ToShortDateString());
runQueryDefinition.RunQueryParameters.Add(rqp);
}
return View(runQueryDefinition);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Run(RunQueryDefinition parameterList)
{
DateTime BeginDate;
DateTime EndDate;
try
{
//QueryDefinition querydefinition = db.QueryDefinitions.Find(id);
if (ModelState.IsValid)
{
return RedirectToAction("Index");
}
}
catch (RetryLimitExceededException)
{
//Log the error
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View();
}
查看
@model BAQueryTool.Models.RunQueryDefinition
@{
ViewBag.Title = "Run Query";
}
<h2>Run Query Definition '@Html.DisplayFor(model => model.QueryDefinition.Name)'</h2>
@using (Html.BeginForm("Run", "QueryDefinition", FormMethod.Post, null))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.RunQueryParameters)
</dt>
<dd>
<table class="table">
<tr>
<th>Parameter Name</th>
<th>Parameter Data Type</th>
<th>Parameter Value</th>
</tr>
@foreach (var item in Model.RunQueryParameters)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.DataType)
</td>
<td>
@Html.EditorFor(modelItem => item.Value)
</td>
</tr>
}
</table>
</dd>
</dl>
<div class="form-group">
<div class="col-md-offset-1 col-md-10">
<input type="submit" value="Run Query" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
主要的類是QueryDefinition類。有一個列出所有查詢定義的索引頁面。當用戶點擊「運行」鏈接時,將生成運行頁面並使用QueryDefinitions填充ID。 QueryParameters集合也被填充以及RunQueryParameters集合。當用戶輸入列值的值並單擊運行查詢時,會發生POST。在調試中,我注意到RunQueryDefinitions parameterList參數沒有完全填充,特別是集合QueryParameters和RunQueryParameters。
有什麼建議嗎?
模型結合的作品嚐試使用@ Html.HiddenFor(modelItem = > item.Name) – Nikitesh 2014-11-25 13:30:19