Share via


How to pass the value in main view to partial view when I using Html.RenderAction

Question

Thursday, January 27, 2011 6:17 PM

Let's say there is a textbox and a dropdownlist in mainview page, these two values are used by  three partial views as well. when I use Html.RenderAction to post action to those three partial views, how to pass these two values to those partial views and in those views how to get these two values as a part of a object to send to database.

Thanks in advance!

All replies (10)

Tuesday, February 1, 2011 10:40 PM âś…Answered

Hi Howexg2,

You need to know that Html.RenderAction will invoke the specific child action and inline in the parent view, if the child action is a post method, you can't put it into a main form. This is like you put a form into another form, which is forbidden.

 

Hope this helpful,

Forest Cheng


Thursday, January 27, 2011 7:06 PM

when I use Html.RenderAction to post action to those three partial views, how to pass these two values to those partial views

 

Do you mean you use RenderAction to render those partials?

If yes, the refer this article and read "Passing Values With RenderAction"

http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx


Thursday, January 27, 2011 7:09 PM

Here is another sample. http://stackoverflow.com/questions/3660329/in-asp-net-mvc-2-how-do-i-get-route-values-into-my-navigation-controller-so-i-c 


Thursday, January 27, 2011 8:22 PM

I want to pass these two values to partial view.

My case is:

Three classes:

Timesheet{Date , ProjectNo,EmployeeId, Hours} in TimeSheetController, TimeSheetPartialView

Expense{Date, ProjectNo, EmployeeId, Description, AccountNo, Amount} in ExpenseController, ExpensePartialView

Unit{Date,ProjectNo,EmployeeId, UnitNo, Description, Quantity} in UnitController, UnitPartialView

In MainView(MainController)

I put Date,ProjectNo, EmployeeId at top of the Page

Then I use Html.RenterAction("TimeSheetPartialView","TimeSheet"),Html.RenterAction("ExpensePartialView","Expense"),Html.RenterAction("UnitPartialView","Unit")

Every time, after I enter a set of data in TimeSheet, Expense, Unit partialview to post an action, I want pass Date, ProjectNo, EmployeeId to those partialview as well. but when I check those class, I found Date, ProjectNo, EmployeeId are nothing, that means they are not being sent to those partialview. So how can I get Date, ProjectNo, EmpoyeeId in those partialview. Thanks!

 


Friday, January 28, 2011 1:14 AM

Hi Howexq9,

Everytime when you hit the controller from your view, and you got return to that view again after processing (just like postback in web forms) then you need to pass all the data items like date, project no, employeeid to the view.

In order to pass all that data again you can create a class called viewmodel class where you put all the data that you want to send to your view from controller, and intialize that viewmodel class and set the properties (date, project no etc) and pass it to view. And when you hit the controller in postback then also you need to send that data back to the controller function, in order to get that data back when controller function  returns to view by creating the instance of viewmodel class.

Hope that I answer to your query.

Regards,

Sitanshu


Friday, January 28, 2011 7:13 PM

Hi,

I did the same way, but after I click one partial view submit button,  in its controller, I found the date, projectNo, employeeId value is nothing, the rest field  of the class have value. I still don't know why.

Thanks!


Friday, January 28, 2011 8:43 PM

I would like to see your view and controller's action method.

But my hunch is that you do not have same names (name attribute) for HTML tags as you have defined in your action method parameter list.


Monday, January 31, 2011 1:34 AM

Hi Howexq9,

Did you get any success, I was thinking about the problem that you were facing. Let me know did you include the viewmodel class in the user controls(the control you are passing in render partial function). You must include the viewmodel class in order to access the values in your page.

Just check it, if you are doing that then share your code (view & controller function).


Monday, January 31, 2011 1:47 AM

Hi Howexg,

If you want to post date, projectNo and employeeId to the specific action, the form you submit must contain these fields(input type). For example, in the TimeSheetPartialView, Date, ProjectNo, EmployeeId and Hours fields with the same name must be included. Because every time when client send a post request, MVC strong-type mechanism will construct object according to these fields. So if the date, projectNo, employeeId value is nothing, the rest field  of the class have value, please check these fields are correct or not.


Tuesday, February 1, 2011 3:26 PM

Here is my code, please check what's wrong with it. why I can't get transdate, wbs1 and employeeid in those partial view?  Thanks!

Main View:

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of MobileWebApplication.FieldReportViewModel)" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    FieldReport
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h4>Field Report</h4>
     <%Using Html.BeginForm("FieldReport", "FieldReport", FormMethod.Post)%>
    <div >
    <fieldset>
    <legend>Transaction Date</legend>
    <table id="DateTable">
    <tr>
    <td>
    <script type="text/javascript">
        $(document).ready(function () { $("#TransDate").datepicker({ dateFormat: 'yy-mm-dd' }); }); </script> <input type="text" id="TransDate" name="TransDate" value=""/>
    </td>
      </tr>
    </table>            
    </fieldset>
    </div>
    <div>
   <fieldset >
   <legend >Project Information</legend>
   <table id="ProjectTable" width ="100%">
   <tr>
   <td><b><%= Html.Label("Project:")%></b></td>
   <td><%= Html.DropDownList("WBS1", Model.ProjectList)%></td>
     <td><b><%= Html.Label("Employee:")%></b></td>
      <td><%=Html.Label(Model.EmployeeName)%></td>
   <td><%=Html.Label(Model.Employee)%></td> 
   </tr>
   </table>
   </fieldset>
    </div>
    <div>
    <fieldset >
    <legend >Time Sheet</legend>
       <%Html.RenderAction("TimeSheet", "TimeSheet")%>
    </fieldset>
    </div>
    <div>
    <fieldset >
    <legend >Employee Expense</legend>
      <%Html.RenderAction("EmployeeExpense", "EmployeeExpense")%>
    </fieldset>
    </div>
    <div>
    <fieldset >
    <legend >Unit</legend>
       <%Html.RenderAction("Unit", "Unit")%>
    </fieldset>
    </div>
     <%End Using%>
</asp:Content>

Main Controller:

Public Class FieldReportController
        Inherits System.Web.Mvc.Controller
          <Authorize()>
        Function FieldReport() As ActionResult
            Dim timesheet As New TimeSheet
            Dim employeeexpense As New EmployeeExpense
            Dim unit As New Unit
            Dim transdate As Date
            Dim employee As String
            ViewData("Emp") = "00001"
            employee = ViewData("Emp")
            Return View(New FieldReportViewModel(TransDate, employee, timesheet,      employeeexpense, unit))
            End Function
          End Class
FieldReportViewModel:
Public Class FieldReportViewModel
        Sub New()
         End Sub
    Public Property TimeSheet() As TimeSheet
    Public Property EmployeeExpense() As EmployeeExpense
    Public Property Unit As Unit
    Public Property APExpense As APExpense
    Public Property ProjectList As SelectList
    Public Property Employee As String
    Public Property EmployeeName As String
     Public Property TransDate As Date
         Public Sub New(ByVal _transdatea As Date, ByVal _employee As String, ByVal _timesheet As TimeSheet, ByVal _employeeexpense As EmployeeExpense, _
                   ByVal _unit As Unit)
        Dim _projectservice As New ProjectService
        Dim _employeeservice As New EmployeeService
        TransDate = _transdatea
        TimeSheet = _timesheet
        Employee = _employee
        EmployeeExpense = _employeeexpense
        Unit = _unit
        ProjectList = New SelectList(_projectservice.GetProjectByEmployee(Employee), "WBS1", "Name")
        EmployeeName = (_employeeservice.GetEmployeeNameByID(Employee).LastName & "," & _employeeservice.GetEmployeeNameByID(Employee).FirstName).ToString

    End Sub
    Public Sub New(ByVal _timesheet As TimeSheet)
        TimeSheet = _timesheet
    End Sub
    Public Sub New(ByVal _employeeexpense As EmployeeExpense)
        EmployeeExpense = _employeeexpense
    End Sub
    Public Sub New(ByVal _unit As Unit)
        Unit = _unit
    End Sub
    Public Sub New(ByVal _apexpense As APExpense)
        APExpense = _apexpense
    End Sub

End Class

TimeSheet Partial View:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of BHS.Vision.FieldApplication.Model.TimeSheet)" %>

<%  Using Html.BeginForm("TimeSheet", "TimeSheet", FormMethod.Post)%>
    <p>
       <%-- <%= Html.ActionLink("Create New", "Create")%>--%>     
    </p>
    <%= Html.ValidationSummary(True) %>
    <table>
        <tr>
                      <th>
                Phase
            </th>
            <th>
               Task
            </th>
            <th >
                Labor Code
            </th>
            <th>
                Regual Hours
            </th>
            <th>
               Overtime Hours
            </th>
            <th>
                Special Overtime Hours
            </th>
            <th>
                Comment
            </th>
        </tr>
        <tr>
                      <td class="editor-field" style="width:10%">
              <%= Html.TextBoxFor(Function(model) model.WBS2, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.WBS2)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.WBS3, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.WBS3)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.LaborCode, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.LaborCode)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.RegHrs, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.RegHrs)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.OvtHrs, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.OvtHrs)%>
            </td>
            <td class="editor-field" style="width:10%">
                <%= Html.TextBoxFor(Function(model) model.SpecialOvtHrs, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.SpecialOvtHrs)%>
            </td>
            <td class="editor-field" style="width:100%">
               <%= Html.TextBoxFor(Function(model) model.TransComment, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.TransComment)%>
            </td>
            <td align ="right" >
            <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>

     <% End Using %>

TimeSheet Controller:

Public Class TimeSheetController
        Inherits System.Web.Mvc.Controller
        <Authorize()>
        Function TimeSheet() As ActionResult
            Dim ts = New TimeSheet
            Return View(ts)
        End Function
        <AcceptVerbs(HttpVerbs.Post), Authorize()>
        Function TimeSheet(ByVal ts As TimeSheet) As ActionResult
             Dim _TimeSheetService = New TimeSheetService
            _TimeSheetService.AddTimeSheet(ts)
            Return View(ts)
        End Function
    End Class

TimeSheet Class:

TimeSheet Class:Public Class TimeSheet
    Public Property TSBatchNo As Integer
    Public Property Employee As String
    Public Property TransDate As Date
    Public Property WBS1 As String
    Public Property WBS2 As String
    Public Property WBS3 As String
    Public Property LaborCode As String
    Public Property RegHrs As Decimal
    Public Property OvtHrs As Decimal
    Public Property SpecialOvtHrs As Decimal
    Public Property TransComment As String = ""
    Public Property Status As String = "N"
    Public Property AuthorizedBy As String
    Public Property RejectReason As String
    Public Property ModDate As Date = Date.Now
    Public Sub New()
    End Sub
    Public Sub New(ByVal tsbatchno As Integer, ByVal employee As String, ByVal transdate As Date, ByVal wbs1 As String, _
                   ByVal wbs2 As String, ByVal wbs3 As String, ByVal laborcode As String, ByVal reghrs As Decimal, _
                   ByVal ovthrs As String, ByVal specialovthrs As Decimal, ByVal transcomment As String, ByVal status As String, _
                   ByVal authorizedby As String, ByVal rejectreason As String, ByVal moddate As Date)
        Me.TSBatchNo = tsbatchno
        Me.Employee = employee
        Me.TransDate = transdate
        Me.WBS1 = wbs1
        Me.WBS2 = wbs2
        Me.WBS3 = wbs3
        Me.LaborCode = laborcode
        Me.RegHrs = reghrs
        Me.OvtHrs = ovthrs
        Me.SpecialOvtHrs = specialovthrs
        Me.TransComment = transcomment
        Me.Status = status
        Me.AuthorizedBy = authorizedby
        Me.RejectReason = rejectreason
        Me.ModDate = moddate
    End Sub
End Class

Expense Partial View:

<%@ Control Language="VB" Inherits="System.Web.Mvc.ViewUserControl(Of BHS.Vision.FieldApplication.Model.EmployeeExpense)" %>

<%  Using Html.BeginForm("EmployeeExpense", "EmployeeExpense", FormMethod.Post)%>
        <%= Html.ValidationSummary(True) %>
    <table>
        <tr>
            <th>
                Phase
            </th>
            <th>
               Task
            </th>
            <th>
               Description
            </th>
            <th >
                Account
            </th>
            <th >
                Amount
            </th>
           <th>
                TaxCode
            </th>
            <th>
               SuppressBill
            </th>
            
            <th>
                ReportDate
            </th>
             <th>
                ReportName
            </th>
        </tr>
        <tr>
            <td class="editor-field" style="width:10%">
              <%= Html.TextBoxFor(Function(model) model.WBS2, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.WBS2)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.WBS3, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.WBS3)%>
            </td>
            <td class="editor-field" style="width:20%">
               <%= Html.TextBoxFor(Function(model) model.Description, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.Description)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.Account, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.Account)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.Amount, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.Amount)%>
            </td>
            <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.TaxCode, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.TaxCode)%>
            </td>
            <td class="editor-field" style="width:10%">
                <%= Html.TextBoxFor(Function(model) model.SuppressBill, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.SuppressBill)%>
            </td>
            
             <td class="editor-field" style="width:10%">
                <script type="text/javascript">
                    $(document).ready(function () { $("#ReportDate").datepicker({ dateFormat: 'yy-mm-dd' }); }); </script> <input type="text" id="ReportDate" name="ReportDate" value="" style="width:auto "/>
                <%= Html.ValidationMessageFor(Function(model) model.ReportDate)%>
            </td>
             <td class="editor-field" style="width:10%">
               <%= Html.TextBoxFor(Function(model) model.ReportName, New With {.style = "width:100%"})%>
                <%= Html.ValidationMessageFor(Function(model) model.ReportName)%>
            </td>
            <td align ="right" >
            <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>

    <%End Using%>

Expense Controller:

Public Class EmployeeExpenseController
        Inherits System.Web.Mvc.Controller
        <Authorize()>
        Function EmployeeExpense() As ActionResult
            Dim emex = New EmployeeExpense
            Return View(emex)
        End Function
        <AcceptVerbs(HttpVerbs.Post), Authorize()>
        Function EmployeeExpense(ByVal emex As EmployeeExpense) As ActionResult
            Dim _EmployeeExpenseService = New EmployeeExpenseService
            _EmployeeExpenseService.AddEmployeeExpense(emex)
            Return View(emex)
         End Function
    End Class

Expense Class:

Public Class EmployeeExpense
    Public Property EmExBatchNo As Integer
    Public Property Employee As String
    Public Property TransDate As Date
    Public Property WBS1 As String
    Public Property WBS2 As String
    Public Property WBS3 As String
    Public Property Account As String
    Public Property Amount As Decimal
    Public Property Description As String
    Public Property SuppressBill As String = "N"
    Public Property TaxCode As String = "GST"
    Public Property ReportDate As Date
    Public Property ReportName As String
    Public Property Status As String = "N"
    Public Property AuthorizedBy As String
    Public Property RejectReason As String
    Public Property ModDate As Date = Date.Now

    Public Sub New()
    End Sub
    Public Sub New(ByVal emexbatchno As Integer, ByVal employee As String, ByVal transdate As Date, ByVal wbs1 As String, ByVal wbs2 As String, _
                   ByVal wbs3 As String, ByVal account As String, ByVal amount As Decimal, ByVal description As String, _
                   ByVal report As Date, ByVal reportname As String, ByVal status As String, ByVal authorizedby As String, _
                   ByVal rejectreason As String, ByVal moddate As Date)
        Me.EmExBatchNo = emexbatchno
        Me.Employee = employee
        Me.TransDate = transdate
        Me.WBS1 = wbs1
        Me.WBS2 = wbs2
        Me.WBS3 = wbs3
        Me.Account = account
        Me.Amount = amount
        Me.Description = description
        Me.SuppressBill = "N"
        Me.TaxCode = "GST"
        Me.ReportDate = ReportDate
        Me.ReportName = reportname
        Me.Status = status
        Me.AuthorizedBy = authorizedby
        Me.RejectReason = rejectreason
        Me.ModDate = moddate
    End Sub
End Class

Unit is same as timesheet and expense.