-1

In MVC, when I post the form, the model data is not posted, I can see the input fields properly named using inspect element. Please help me out....

Following is my code

Using (Html.BeginForm("_saveActivities", "Projects", FormMethod.Post, New With {.name = "frmActivity", .id = "frmActivity", .enctype = "multipart/form-data"}))    
        @Html.HiddenFor(Function(X) X.Project.ID)
        @<table class="formTable">
                <thead>
                    <tr>
                        <td colspan="2">Project Activties</td>
                    </tr>
                </thead>
                <tr>
                    <td colspan="2" style=" text-align:right; background-color:#DFDFDF">  
                        To add new row click 
                        <a href="#?" onclick="addActivityRow(@Model.Project.ID)">click here</a>
                    </td>
                </tr>                    
                <tr>
                    <td colspan="2" style=" background-color:#DFDFDF">
                        <table id="tblProjectActivities" style=" width:100%">
                           <tr>
                                <td  style="width:1px;"></td>
                                <td  style=" width:300px;" >Activity</td>
                                <td  style=" width:110px;" >Start Date</td>
                                <td  style=" width:110px;" >End Date</td>
                                <td style=" width:15px;"></td>
                            </tr>
                            @For Each Item In Model.ProjectActivities
                                    Html.RenderPartial("_ProjectFormActivitiesRow", Item)
                                Next
                        </table>
                    </td>
                </tr>               
                <tfoot>
                    <tr>
                        <td colspan="2">
                            <div id="buttonsActivityDiv" style=" position:relative; background-color:inherit; height:40px;">
                                <input type="submit" id="comActivity" value="Update Activities"  class="k-button"/>
                           </div>
                        </td>
                    </tr>
                </tfoot>
        </table> 

    End Using

In parialview I have following code

@ModelType Models.ProjectActivityItem 
@code
    Dim strID As String = Guid.NewGuid.ToString
    Dim strRowID As String = Guid.NewGuid.ToString
    Dim strStateID As String = Guid.NewGuid.ToString("N")
End Code
<tr id="@strRowID">
    <td>
        <input type="hidden"  name="ProjectActivties.Index" value="@strID" />
        <input type="hidden"  name="ProjectActivties[@(strID)].DetailEntity.ID" value="@Model.DetailEntity.ID"/>
        <input type="hidden"  name="ProjectActivties[@(strID)].DetailEntity.CreatedOn" value="@Model.DetailEntity.CreatedOn"/>
        <input type="hidden"  name="ProjectActivties[@(strID)].DetailEntity.CreatedBy" value="@Model.DetailEntity.CreatedBy"/>
        <input type="hidden"  name="ProjectActivties[@(strID)].DetailEntity.VersionEncoded" value="@Model.DetailEntity.VersionEncoded"/>
        <input type="hidden"  name="ProjectActivties[@(strID)].DetailEntity.ProjectID" value="@Model.DetailEntity.ProjectID"/>
        <input type="hidden" id="@strStateID"  name="ProjectActivties[@(strID)].DetailEntity.EntityState" value="@Model.DetailEntity.EntityState"/>
    </td>   
    <td>@Html.TextBox("ProjectActivties[" & strID & "].DetailEntity.ActivityDescription", Model.DetailEntity.ActivityDescription, New With {.class = "inputAreaFull k-textbox"})</td>
    <td>@Html.Kendo.DatePicker().Name("ProjectActivties[" & strID & "].DetailEntity.StartDate").Min(New DateTime(2000, 1, 1)).Value(Model.DetailEntity.StartDate).Format("dd-MMM-yyyy").HtmlAttributes(New With {.style = "width:110px"})</td>
    <td>@Html.Kendo.DatePicker().Name("ProjectActivties[" & strID & "].DetailEntity.EndDate").Min(New DateTime(2000, 1, 1)).Value(Model.DetailEntity.EndDate).Format("dd-MMM-yyyy").HtmlAttributes(New With {.style = "width:110px"})</td>
    <td>
      <img src="@Url.Content("/Content/delete_icon.png")" onclick="removeProjectActivity('@strRowID','@strStateID')" style ="cursor:pointer" title="Delete Detail"/>
    </td>
 </tr>         

and in controller, I am using

<HttpPost()> _
    Function _saveActivities(lModel As Models.ProjectModel) As ActionResult

    End Function

Following is my model.

Public Class ProjectActivity

        Public Property ID As Int64
        Public Property ProjectID As Int32
        Public Property ActivityDescription As String
        Public Property StartDate As DateTime
        Public Property EndDate As DateTime
        Public Property CreatedOn As DateTime
        Public Property CreatedBy As Int32
        Public Property ModifiedOn As DateTime
        Public Property ModifiedBy As Int32

        <NotMapped()> _
        Public Property VersionEncoded As String
        <NotMapped()> _
        Public Property EntityState As EntityStateEnum
    End Class


 Public Class ProjectActivityItem
        Inherits BaseObject

        Public Property DetailEntity As Database.ProjectActivity            
    End Class
  • 1
    You have not shown you model, or the view, or the html your generating. What makes you think your input fields are properly named. I can almost guarantee you that they are not since are but default a partial wont work for a collection. –  Jul 28 '15 at 11:25
  • Could you please post the complete template? :) Ie. you have a
    -tag, right?
    – Stefan Herijgens Jul 28 '15 at 11:26
  • I have edited the post, if you can check it. – Naeem Ullah Jul 28 '15 at 11:35
  • Which model have you shown? It does not relate to any code in your view. Show the main model, and the `ProjectActivityItem` model used in your partial and model described by property `DetailEntity` –  Jul 28 '15 at 11:37
  • Thanks Stephen, I have added that model as well. – Naeem Ullah Jul 28 '15 at 11:43
  • You still have not show the main model (the one with a property named `ProjectActivties`. But scrap all this awful code and do it correctly using strongly typed html helpers. Use `for` loops in the main view (the only manual html you should be creating is for the hidden input for the `Index` property) or use the `BeginCollectionItem` helper so that your controls are correctly named, you get proper 2-way model binding, client side validation etc –  Jul 28 '15 at 11:52
  • Refer the answers [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) and [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) for some examples –  Jul 28 '15 at 11:53
  • I have edited the code, please review it and let me know if anything else is required. – Naeem Ullah Jul 28 '15 at 12:04
  • Please read my previous comments. According to you POST method, your main model is `ProjectModel` but you still have not shown it. (And based on your view, `ProjectModel` contains a property `ProjectActivties` which is typeof `IEnumerable`) –  Jul 28 '15 at 12:21

1 Answers1

0

The model does not map with control's Id . It mapped With name. check your rendered control name & model property are same of different .

Kaushik Thanki
  • 3,334
  • 3
  • 23
  • 50