2 Replies - 490 Views - Last Post: 09 October 2016 - 07:58 PM

#1 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Update model with form POST

Posted 08 October 2016 - 08:24 AM

Posted Image
I want to keep the current model after a form post but my Prie and OnsaleValues get reset after a POST
http://localhost/Wid...pDownQuantity=2

How do I perform and operation on all the information on the page and still be able to add data to it from the user.

Maybe I'm going about this completely wrong. I don't want to use JS.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Widgit.Domain.Models;

namespace Widgit.Domain.Controllers
{
  public class HomeController : Controller
  {
    // GET: Home
    public ActionResult Index()
    {
      Item item = new Item();
      item.Name = "Lamp";
      item.Price = 29.99m;
      item.OnSale = true;
      ViewBag.DropDownQuantity = new SelectList(new[]
      { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
      ViewBag.DropDownStates = new SelectList(new[]
      { "AK", "AL", "AR", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VA", "VT", "WA", "WI", "WV", "WY" });
      return View(item);
    }
    [HttpPost]
    public ActionResult Index(Item item)
    {      
      return RedirectToAction("Index", item);
    }
  }
}


@{
  Layout = null;
}
@model Widgit.Domain.Models.Item
<!DOCTYPE html>

<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title></title>
</head>
<body>
  <div>

    @Model.Name
    $@Model.Price
    @if(@Model.OnSale == true){
      <label>On Sale!</label>
    }
   
    <br/>
    @using (Html.BeginForm())
    {
      <fieldset>
        <legend>Form</legend>
        <p>
          @Html.DropDownListFor(m => m.DropDownQuantity, Model.Quantity, "Select Quantity")
        </p>
        <p>
          State: @Html.DropDownList("DropDownStates", (SelectList)ViewBag.DropDownStates)
        </p>
        <p>
          <input type="submit" value="Calculate" />
        </p>
      </fieldset>
    }
    @Html.Label("label", Model)  </div>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Widgit.Domain.Models
{
  public class Item
  {
    public string Name{ get; set; }
    public decimal Price{ get; set; }
    public bool OnSale { get; set; }

    public string DropDownQuantity { get; set; }
    public SelectList Quantity { get; set; }

  }
}

This post has been edited by wtp: 08 October 2016 - 08:26 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Update model with form POST

#2 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Re: Update model with form POST

Posted 08 October 2016 - 10:48 AM

I just needed HiddenFors in my form to pass model values back to the controller

@Html.HiddenFor(m => m.Name)
Was This Post Helpful? 0
  • +
  • -

#3 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Re: Update model with form POST

Posted 09 October 2016 - 07:58 PM

Edit: Ok so accepting a list in my controller works. But now I can't see the DropDownList values.
[HttpPost]
    public ActionResult Index(List<Item> item)


How can I rewrite
@Html.DropDownList("DropDownStates", "Select State")

In order for it to be in the list?
-------
But now when I try to do it in a for loop the values aren't posted.
@Html.HiddenFor(m => Model[i].Price)


Model[i].Price shows the correct value, but it not passing it to the POST method.

The Html helper is producing <input> ... name="[0].Price" ... </inuput>
When I want <input> ... name="Price" ... </inuput>

[email protected] (int i = 0; i < Model.Count; i++)
  {
    <p>
      @Html.DisplayFor(m => Model[i].Name)
      @Html.DisplayFor(m => Model[i].Price)
      @if (Model[i].OnSale == true)
      {
        <label>On Sale!</label>
      }
    </p>

    using (Html.BeginForm())
    {
      <p>
        @Html.Hidden(Model[i].Name)
        @Html.HiddenFor(m => Model[i].Price)
        @Html.HiddenFor(m => Model[i].OnSale)

        @Html.DropDownList("DropDownQuantity", "Select Quantity")
        @Html.DropDownList("DropDownStates", "Select State")

        @*@Html.DropDownListFor(m => element.DropDownQuantity, element.Quantity, "Select Quantity")*@
      </p>
      <p>
        <input type="submit" value="Calculate" />
      </p>
      if (@HttpContext.Current.Request.QueryString["FinalPrice"] != null)
      {
        <text>Total: $</text>@HttpContext.Current.Request.QueryString["FinalPrice"]

      }
    }
  }...

This post has been edited by wtp: 10 October 2016 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1