Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi!

I have some difficult time to pass data to Partial view httpPost ActionResult method in Controller.

Let me explain:

1.Have entity which i have been generated with Entity Framework ORM and this looks like this.
<pre lang="c#">

C#
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Brasken.Entities
{
    using System;
    using System.Collections.Generic;

    public partial class bras_AllaCarte
    {
        public int AllaCarteID { get; set; }
        public string AllCarteInformation { get; set; }
    }
}



2. I have MenyAdminController with folowing methods:

C#
public ActionResult RooleThem()
     {
         return View();
     }

    [HttpGet , ChildActionOnly]
    public ActionResult _AllaCarte(int id=1)
    {
        var bras_allacarte = db.bras_AllaCarte.ToList();
        return PartialView("_AllaCarte", bras_allacarte);
    }



     [HttpPost]
     [ValidateInput(false)]
     public ActionResult _AllaCarte(FormCollection formData, bras_AllaCarte model)
     {
         //var model = TempData["tempAllaCarte"] as bras_AllaCarte;
         if (ModelState.IsValid)
         {
             try
             {
                 db.Entry(model).State = EntityState.Modified;
                 db.SaveChanges();

                 return PartialView("_AllaCarte", model);
             }
             catch (DataException)
             {
                 ModelState.AddModelError("", "Kunde inte updatera,Prova  igen om problemet                 uppstår igen var god kontakta administratoren.");
             }
         }
         return View();
     }



The porpoise of this methods are to use them for administration of brass_AllaCarte entity thru partial view _AllACarte.cshtml , and this looks as folowing:







C#
  @model IEnumerable<Brasken.Entities.bras_AllaCarte>
 <div class="jumbotron">

    @using (Html.BeginForm("_AllaCarte", "MenyAdmin" , FormMethod.Post, null))
    {
       var item = Model.First();
    
        @Html.HiddenFor(modelItem => item.AllaCarteID)
        @Html.TextAreaFor(modelItem => item.AllCarteInformation);


    <ul>
        
        <li><a  önclick="updateAllaCarteMeny()" class="arrow">Updatera</a></li>
   
    </ul>
         <input style="display:none" id="updateAllaCarte" type="submit" value="" />
    

    }
</div>



And att last i have RollThem.cshtml view where i calling a Partial view , and this looks
like this:


C#
  <pre lang="xml">@{
    ViewBag.Title = "RooleThem";
    Layout = "~/Views/Shared/_Layout.cshtml";
}







    @{Html.RenderAction("_AllaCarte", "MenyAdmin");}



Problem:

When i debug a  project  , im geting a right  data  , but  when i need  to save  changes  with HttpPost method  model data  is  null.

What  im doing  wrong?
Posted

1 solution

In your view change the IEnumerable to List, that solved a similar issue I had.

C#
@model List<brasken.entities.bras_allacarte>


IEnumerables are readonly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900