POST后,ViewModel部分为空

时间:2014-11-25 13:16:43

标签: asp.net-mvc asp.net-mvc-4

我有一个MVC 4应用程序,其中我有一个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集合。当用户输入Column Value的值并单击Run Query时,将发生POST。在Debug中,我注意到RunQueryDefinitions parameterList参数未完全填充,尤其是集合QueryParameters和RunQueryParameters。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

不要使用foreach循环,而是使用for循环,以便可以发布集合的索引值:

@for(int i = 0; i < Model.RunQueryParameters.Count; i++)
                    {
                        <tr>
                            <td>
                                @Html.DisplayFor(modelItem => Model.RunQueryParameters[i].Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => Model.RunQueryParameters[i].DataType)
                            </td>
                            <td>
                                @Html.EditorFor(modelItem => Model.RunQueryParameters[i].Value)
                            </td>
                        </tr>
                    }

了解模型绑定在this article.

中的工作原理