0

I am gonna submit the form everytime when checkbox clicked later but for now I need to pass the data if checkboxes are false or true but data never goes to my PriceRange post method. Thanks.

this is my controller. I pass data to my view with this obj.

public class HomeController : Controller
{
    private FilizTakiEntityDB db = new FilizTakiEntityDB();
    public ActionResult Index()
    {
        Anasayfa obj = new Anasayfa();

        obj.products = db.Products.ToList();
        obj.priceRange = db.PriceRange.ToList();

        return View("Index", obj);
    }

    [HttpPost]
    public ActionResult PriceRange(PriceRange prc , int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var mevcut = db.PriceRange.Find(id);
        mevcut.Fiyat1 = prc.Fiyat1;
        mevcut.Fiyat2 = prc.Fiyat2;
        mevcut.Fiyat3 = prc.Fiyat3;
        return View("Index");
    }

}
public class Anasayfa
{
    public List<PriceRange> priceRange { get; set; }
    public List<Products> products { get; set; }
}

this is my view:

  @model  FilizTakiProject.Controllers.Anasayfa

  @using (Html.BeginForm("PriceRange", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                foreach (var x in Model.priceRange)
                {
                    @Html.CheckBoxFor(ModelItem=>x.Fiyat1)<br />
                    @Html.CheckBoxFor(ModelItem => x.Fiyat2)<br />
                    @Html.CheckBoxFor(ModelItem => x.Fiyat3)
                }
                <input type="submit" name="submit" value="Submit" />
            }

this is my model

 public partial class PriceRange
   {
    public int PriceID { get; set; }
    public bool Fiyat1 { get; set; }
    public bool Fiyat2 { get; set; }
    public bool Fiyat3 { get; set; }
   }
  • May you provide the code from PriceRange class so we can have more info to help? Thanks. – dime2lo Oct 30 '16 at 19:59
  • I just added. If i use only PriceRange model in the View its working fine. But i want to use this approach for multiple models – Burak Ipek Oct 30 '16 at 20:17
  • Correct me if I´m wrong but the PriceRange action shouldn´t receive a List of PriceRange? – dime2lo Oct 30 '16 at 20:32
  • you are right but i dont know what should it receive. I have tried Anasayfa prc but it also comes null. My main question is this actually, what should PriceRange action receieve? thank you – Burak Ipek Oct 30 '16 at 20:36
  • 1
    You cannot use `foreach` loops to generate form controls for collections - you need a `for` loop or custom `EditorTemplate` for typeof `PriceRange` and the parameter in the POST method needs to be `Anasayfa` (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) - the same model that you use in the view –  Oct 30 '16 at 20:36
  • Thank you so much for your help. I fixed it – Burak Ipek Oct 30 '16 at 20:57

0 Answers0