-1

I am trying to figure out how to get my data that I've called to my Approve view to post back after any changes have been made. So far the things that I have tried have either thrown an error or flat out erased/disassociated data.

Here is my 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; }
}

My Approval View:

    <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>
}

and my controller method:

        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");

    }

I'm starting to think that my issue may have something to do with either me trying to use my IQueryable to post the SurplusItem data back is the problem or the fact that I just been to post them back in a different way all together.

Any advice would help. Thanks

  • What do you want, to get an item and to update an item? Because insert is not possible, the way you structured the methods. – Nikolaus Mar 16 '18 at 21:02
  • I'm just updating the request table with names and dates of the people approving the pickup. The fields are already attached, just null waiting to be updated. – Joshua Nickens Mar 16 '18 at 21:23
  • What in the world are you trying to do here. There is nothing in your form which is editable so remove all you form controls (they are disabled and disabled controls do not submit a value, and you hidden inputs in the loop have no relationship at all to your model because you cannot use a `foreach` loop to generate them). Refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) for a detailed explanation –  Mar 17 '18 at 02:46
  • Even if you did generate your inputs correctly so that you model would be bound - it is pointless and degrading the performance of you app by generating all that extra html, sending it to the client, and then sending it all back again unchanged. And then for most of you properties you have both a disabled input and a pointless hidden input. –  Mar 17 '18 at 02:51
  • Actually, there are editable fields there in my view. What I am doing is making a single view where people who need to approve the surplus inventory can come and review the form, leave their name and press approve which would submit their name and date back to the form. The if statement then checks on reload whether the last person has approved and if they have they it will disable the previous entry point for the sake of integrity and reveal where they need to name, date and press approve. Rinse and repeat until everyone has approved and then lock the form as view only. – Joshua Nickens Mar 17 '18 at 14:23
  • The you `
    ` need one form control only - for the name of the person, plus a route value for the ID of the object.
    –  Mar 17 '18 at 23:43
  • That ended up doing it. I got tunnel vision there and was trying to do everything from the ViewModel. Thank You – Joshua Nickens Mar 19 '18 at 15:38

2 Answers2

0

Put those view under a Form.

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

Any changes you made on your View Model will automatically bind and will reflect your form post.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • I have that in my actual code. I just left it off of the description here description because the site's code vetting kept yelling at me. I'm 80% sure the issue is at the post controller but I'm not sure what it is that I'm not doing. – Joshua Nickens Mar 16 '18 at 21:25
0

lets break down your problems

1st cince you didnt provided the whole view i believe you dont have a form tag

 @using (Html.BeginForm())
    {

    }

now if you have this even then you getting the error which you should

then you are missing the antiforgerytocken

@AntiForgeryTocken();

inside your form

now this antiforgery tocken will be needed since you are using

[ValidateAntiForgeryToken] attribute in the post acton

i guess this should do

====update ======

I think i got your issue mate

if its jsut simple postback then try to change your action method like this

 public ActionResult Approvals SurplusRequest SR,int? id null)

i am not sure y are u using int?id here since its a strongly typed view it wll only accept the model itself not other params

RAHUL S R
  • 1,569
  • 1
  • 11
  • 20
  • I have that in my code, I just left it off in my example for the sake of getting everything to post properly. – Joshua Nickens Mar 16 '18 at 21:31
  • I've had different ones but this is the one attributed to the most recent thing I've tried in the code above: The entity type DbQuery`1 is not part of the model for the current context. – Joshua Nickens Mar 16 '18 at 21:35
  • then its hitting the action right guess you messed up wth ef check your datacontext – RAHUL S R Mar 16 '18 at 21:37