0

View

@if (weekMaster != null)
 {
  using (Html.BeginForm("UpdatePlan", "generalPlan", FormMethod.Post, new {  }))
  {
  <table class="table-bordered">
   <tr>
   @foreach (TermMaster obj in weekMaster.ToList())
    {
    <td align="center">
   <span>  @obj.termStartDate.ToString("dd MMM") - @obj.termEndDate.ToString("dd MMM")</span>
    <br />
    <input type="hidden" name="ObjHid" value="@obj" />
    <input type="hidden" name="startDate" value="@obj.termStartDate" />
    <input type="hidden" name="endDate" value="@obj.termEndDate" />
    <input type="text" style="width:80%" name="weekSession" />
  </td>
 }
  <td>
 <input type="submit" value="Update" class="btn-primary" />
  </td>
 </tr>
 </table>
 } }

Controller

   [HttpPost]
    public ActionResult UpdatePlan(List<DateTime> startDate, List<DateTime> endDate, List<int> weekSession, List<TermMaster> ObjHid)
    {
        return View();
    }

I am trying pass Class Object from View to Controller above TermMaster class Object pass using input method <input type="hidden" name="ObjHid" value="@obj" /> but showing NULL value if pass single value like startDate and endDate then it work fine.

What is wrong in my code? how to pass class object List in Post Method?

Please refer Image enter image description here

User
  • 1,334
  • 5
  • 29
  • 61
  • 4
    Suggest you refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Jul 26 '17 at 06:58
  • 1
    But what is the point of degrading the performance of your application by attempting to send back a collection of `TermMaster` that your not even editing –  Jul 26 '17 at 07:05
  • @StephenMuecke in post method I am inserting some parameter value with textbox value for that I need some value from TermMaster – User Jul 26 '17 at 07:10
  • 2
    Sorry, that makes no sense. Are you saying that `TermMaster` has a property which is related to ``? And read the link to understand how to generate a view for a collection. –  Jul 26 '17 at 07:12

2 Answers2

0

You have to do it by below approach.

Create a model instead of multiple parameters, and use index in cshtml.

public class model
{
    public List<DateTime> startDate { get; set; }
    public List<DateTime> endDate { get; set; }
    public List<int> weekSession { get; set; }
    public List<TermMaster> ObjHid { get; set; }
}

CSHTML

@{ int i = 0; }
@foreach (TermMaster obj in weekMaster.ToList())
    {
    <td align="center">
   <span>  @obj.termStartDate.ToString("dd MMM") - @obj.termEndDate.ToString("dd MMM")</span>
    <br />

    <input type="hidden" name="ObjHid[@i].termStartDate" value="@obj.termStartDate.ToString("dd MMM")" />
    <input type="hidden" name="ObjHid[@i].termStartDate" value="@obj.termStartDate.ToString("dd MMM")" />

    <input type="hidden" name="startDate[@i]" value="@obj.termStartDate" />
    <input type="hidden" name="endDate[@i]" value="@obj.termEndDate" />
    <input type="text" style="width:80%" name="weekSession[@i]" />
  </td>

   i++
 }
Ankush Jain
  • 5,654
  • 4
  • 32
  • 57
  • 2
    Wrong - A `` only posts back a single value and `obj` is a complex object (your first input generates `value="AssemblyName.TermMaster"` which cannot bind to a complex object. –  Jul 26 '17 at 07:07
  • @StephenMuecke I have amended my answer..:) – Ankush Jain Jul 26 '17 at 07:25
  • Do you still think my answer is wrong. I know how these binding works. Name attribute is mapped with property. I am not sure about startDate , endDate but ObjHid will surely get mapped. – Ankush Jain Jul 26 '17 at 07:34
0

You can not bind objects to controller from your input. You can serialize object to json. In the controller you can take your inputs value as string and deserialize it.

mehmet baran
  • 636
  • 8
  • 18