发布编辑ViewModel数据

时间:2018-03-16 20:51:47

标签: c# asp.net-mvc http-post viewmodel

我正在尝试弄清楚如何获取我已调用我的Approve视图的数据,以便在进行任何更改后回发。到目前为止,我尝试的事情要么抛出错误,要么消除已删除/无关联的数据。

这是我的ViewModel:

    public class SurplusRequest
{
    public int PickUpID { get; set; }

    [DisplayName("First Name:")]
    public string EmployeeFN { get; set; }

    [DisplayName("Last Name:")]
    public string EmployeeLN { get; set; }

    [DisplayName("Phone:")]
    [DataType(DataType.PhoneNumber)]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid Telephone Number")]
    public string EmployeePhone { get; set; }

    [DisplayName("Request Date:")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
    public DateTime? RequestDate { get; set; }

    [DisplayName("Department:")]
    public string Department { get; set; }

    [DisplayName("Pick Up Requested By:")]
    public string PURequestedBy { get; set; }

    [DisplayName("Requested Pick Up Date:")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
    public DateTime? PURequestedDate { get; set; }

    [DisplayName("Division Manager:")]
    public string DivisionHead { get; set; }

    [DisplayName("Approval Date:")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
    public DateTime? DivisionHeadDate { get; set; }

    [DisplayName("Surplus Manager:")]
    public string SurplusManager { get; set; }

    [DisplayName("Surplus Approval Date:")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
    public DateTime? SurplusManagerDate { get; set; }

    [DisplayName("Financial Officer:")]
    public string FinancialOfficer { get; set; }

    [DisplayName("Financal Approval Date:")]
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}",
        ApplyFormatInEditMode = true)]
    public DateTime? FinancialOfficerDate { get; set; }


    public PickUpRequest Details { get; set; }
    public List<SurplusItem> SurplusItems { get; set; }
    public IQueryable<SurplusItem> SItems { get; set; }
}

我的审批视图:

    <div>
    <hr />

    <div class="row">
        <div class="col-md-4">
            @Html.LabelFor(model => model.RequestDate, htmlAttributes: new { @class = "control-label col-md-5" })
            @Html.EditorFor(model => model.Details.RequestDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.RequestDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-4">
            @Html.LabelFor(model => model.EmployeeFN, htmlAttributes: new { @class = "control-label col-md-5" })
            @Html.EditorFor(model => model.Details.EmployeeFN, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.EmployeeFN)
        </div>
        <div class="col-md-4">
            @Html.LabelFor(model => model.EmployeeLN, htmlAttributes: new { @class = "control-label col-md-5" })
            @Html.EditorFor(model => model.Details.EmployeeLN, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.EmployeeLN)
        </div>

        <div class="col-md-4">
            @Html.LabelFor(model => model.Department, htmlAttributes: new { @class = "control-label col-md-5" })
            @Html.EditorFor(model => model.Details.Department, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.Department)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-4">
            @Html.LabelFor(model => model.EmployeePhone, htmlAttributes: new { @class = "control-label col-md-5" })
            @Html.EditorFor(model => model.Details.EmployeePhone, new { htmlAttributes = new { @class = "form-control", style = "width:150px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.EmployeePhone)
        </div>
    </div>
</div>

<br />
<br />

<div align="center">
    <div class="row">
        <table class="table table-bordered table-responsive" style="width:auto">
            <tr>
                <th>Description of Surplus Item</th>
                <th>Asset Tag</th>
                <th>Works Y or N</th>
                <th>Price Item Is Worth</th>
                <th># of Items</th>
                <th>Condition of Items</th>
                <th>Marked for Trash Y or N</th>
            </tr>

            @foreach (var item in Model.SItems)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(model => item.Description)
                        @Html.HiddenFor(model => item.Description)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.AssetTag)
                        @Html.HiddenFor(model => item.AssetTag)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.YesNo1.YesNoChoice)
                        @Html.HiddenFor(model => item.YesNo1.YesNoChoice)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.Worth)
                        @Html.HiddenFor(model => item.Worth)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.NumItems)
                        @Html.HiddenFor(model => item.NumItems)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.Condition.ConditionChoice)
                        @Html.HiddenFor(model => item.Condition.ConditionChoice)
                    </td>
                    <td>
                        @Html.DisplayFor(model => item.YesNo.YesNoChoice)
                        @Html.HiddenFor(model => item.YesNo.YesNoChoice)
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

<br />
<br />

<div class="row">
    <div class="col-md-5">
        @Html.LabelFor(model => model.PURequestedBy, htmlAttributes: new { @class = "col-md-5" })
        @Html.EditorFor(model => model.Details.PURequestedBy, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
        @Html.HiddenFor(model => model.Details.PURequestedBy)
    </div>
    <div class="col-md-5">
        @Html.LabelFor(model => model.PURequestedDate, htmlAttributes: new { @class = "col-md-6" })
        @Html.EditorFor(model => model.Details.PURequestedDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", @disabled = "disabled" } })
        @Html.HiddenFor(model => model.Details.PURequestedDate)
    </div>
</div>

<br />

if (Model.Details.DivisionHead == null)
{
    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
            @Html.ValidationMessageFor(model => model.DivisionHead, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:100px", id = "DivisionHeadDate" } })
            @Html.ValidationMessageFor(model => model.DivisionHeadDate, "", new { @class = "text-danger" })
        </div>
    </div>
}
else if (Model.Details.DivisionHead != null && Model.Details.SurplusManager == null)
{
    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHead)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHeadDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
            @Html.ValidationMessageFor(model => model.SurplusManager, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate" } })
            @Html.ValidationMessageFor(model => model.SurplusManagerDate, "", new { @class = "text-danger" })
        </div>
    </div>

}
else if (Model.Details.DivisionHead != null && Model.Details.SurplusManager != null && Model.Details.FinancialOfficer == null)
{
    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHead)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHeadDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.SurplusManager)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.SurplusManagerDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.FinancialOfficer, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.FinancialOfficer, new { htmlAttributes = new { @class = "form-control", style = "width:175px" } })
            @Html.ValidationMessageFor(model => model.FinancialOfficer, "", new { @class = "text-danger" })
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.FinancialOfficerDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.FinancialOfficerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "FinancialOfficerDate" } })
            @Html.ValidationMessageFor(model => model.FinancialOfficerDate, "", new { @class = "text-danger" })
        </div>
    </div>
}
else
{
    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHead, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.DivisionHead, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHead)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.DivisionHeadDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.DivisionHeadDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "DivisionHeadDate", @disabled = "diabled" } })
            @Html.HiddenFor(model => model.Details.DivisionHeadDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManager, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.SurplusManager, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.SurplusManager)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.SurplusManagerDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.SurplusManagerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "SurplusManagerDate", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.SurplusManagerDate)
        </div>
    </div>

    <br />

    <div class="row">
        <div class="col-md-5">
            @Html.LabelFor(model => model.FinancialOfficer, htmlAttributes: new { @class = "col-md-5" })
            @Html.EditorFor(model => model.Details.FinancialOfficer, new { htmlAttributes = new { @class = "form-control", style = "width:175px", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.FinancialOfficer)
        </div>
        <div class="col-md-5">
            @Html.LabelFor(model => model.FinancialOfficerDate, htmlAttributes: new { @class = "col-md-6" })
            @Html.EditorFor(model => model.Details.FinancialOfficerDate, new { htmlAttributes = new { @class = "form-control", style = "width:90px", id = "FinancialOfficerDate", @disabled = "disabled" } })
            @Html.HiddenFor(model => model.Details.FinancialOfficerDate)
        </div>
    </div>
}

<br />
<br />

if (Model.Details.FinancialOfficer == null)
{
    <div class="col-md-1">
        <input type="submit" value="Approve" class="btn btn-success" />
    </div>

    <div class="col-md-2">
        <input type="button" value="Return" class="btn btn-info" onclick="@("window.location.href='" + @Url.Action("SubmittedRequests", "SurplusRequest") + "'");" />
    </div>
}
else
{
    <div class="col-md-2">
        <input type="button" value="Return" class="btn btn-info" onclick="@("window.location.href='" + @Url.Action("SubmittedRequests", "SurplusRequest") + "'");" />
    </div>
}

和我的控制器方法:

        public ActionResult Approvals(int? id)
    {
        var model = new SurplusRequest
        {
            Details = db.PickUpRequests.Find(id),
            SItems = db.SurplusItems.Where(s => s.PickUpID == id)
        };


        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        if (model == null)
        {
            return HttpNotFound();
        }

        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Approvals(int? id, SurplusRequest SR)
    {
        var model = new SurplusRequest
        {
            Details = db.PickUpRequests.Find(id),
            SItems = db.SurplusItems.Where(s => s.PickUpID == id)
        };


        //SurplusRequest SItems = db.SurplusItems.Where(s => s.PickUpID == id);
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        if (model == null)
        {
            return HttpNotFound();
        }

        if (!ModelState.IsValid)
        {
            return View(model);
        }

        db.Entry(model.Details).State = EntityState.Modified;

        foreach (var i in model.SItems)
        {
            db.Entry(model.SItems).State = EntityState.Modified;
        }
        db.SaveChanges();
        return RedirectToAction("SubmittedRequests");

    }

我开始认为我的问题可能与我试图使用我的IQueryable发布SurplusItem数据回来是问题或事实我只是以不同的方式发回它们在一起。

任何建议都会有所帮助。感谢

2 个答案:

答案 0 :(得分:0)

将这些视图放在表单下。

 @using (Html.BeginForm())
        {
          //All your view here
        }

您在视图模型上所做的任何更改都将自动绑定并反映您的表单帖子。

答案 1 :(得分:0)

让我们解决您的问题

第一个cince你没有提供我认为你没有的整个视图  表格标签

 @using (Html.BeginForm())
    {

    }

现在如果你有这个,你就会得到你应该

的错误

然后你错过了antiforgerytocken

@AntiForgeryTocken();
在表单中

现在,因为你正在使用

,所以需要这种防伪装置 帖子中的

[ValidateAntiForgeryToken]属性

我想这应该做

====更新======

我想我得到了你的问题配偶

如果它的jsut简单回发然后尝试改变你的动作方法,就像这样

 public ActionResult Approvals SurplusRequest SR,int? id null)

我不确定你是否在使用int?id,因为它是一个强类型视图,它只接受模型本身而不是其他参数