How to pass data from view to controller Mvc preview 5

Last post 10-14-2008 9:33 AM by Cody Skidmore. 2 replies.

Sort Posts:

  • How to pass data from view to controller Mvc preview 5

    10-14-2008, 8:28 AM
    • Member
      point Member
    • Avani_nayak
    • Member since 10-14-2008, 12:20 PM
    • Posts 6

    Hello,

        I am working on MVC Preview 5.I have a problem passing textbox value form the view to the controller.
        If I use pure HTML controls,passing values would be possible using form submit.
        But, how can I do the same with ASP.Net server controls?
        Can I still access the form control values submitted from the View in the controller class?
     
         this is my code for server control

    1. <asp:Label ID="Label7" runat="server" ForeColor="#FF6600" Style="z-index: 1; left: 17px;top: 155px; position: absolute; width: 85px; height: 17px"   Text="Username:"></asp:Label>
    2. <asp:TextBox ID="txtUsername" runat="server" Style="z-index: 1; left: 15px; top: 180px;position: absolute; width: 163px; bottom: 293px"></asp:TextBox>
    3. <asp:Label ID="Label8" runat="server" Font-Size="Medium" ForeColor="#FF6600" Style="z-index: 1;left: 17px; top: 203px; position: absolute; width: 78px; height: 23px" Text="Password:"></asp:Label>
    4. <asp:TextBox ID="txtPassword" runat="server" Style="z-index: 1; left: 15px; top: 224px;position: absolute; width: 163px; bottom: 227px"></asp:TextBox>
    5. <asp:LinkButton ID="lnkbtnLogin" runat="server" Font-Bold="True" Font-Names="Times New Roman"ForeColor="#FF6600" OnClick="LinkButton1_Click" Style="z-index: 1; left: 60px;top: 257px; position: absolute; width: 64px; height: 23px">Login </asp:LinkButton> 
          
            
           I want to pass the the two textbox values from the view page to the controller on the LinkButton click.
           Following is the link to the article which demonstrates how to pass HTML control values.
       http://www.worldofasp.net/News/AJAX/re-2775_ASP-NET-MVC-Framework-Part-4--Handling-Form-Edit-and-Post-Scenarios.aspx  
           I want to do the same thing using ASP.NET TextBox controls. 
          
        Can anyone help me out with this.

     

    Thanks in advance,
    Avani 

  • Re: How to pass data from view to controller Mvc preview 5

    10-14-2008, 9:24 AM
    • Participant
      1,582 point Participant
    • mohanbrij
    • Member since 08-07-2008, 8:40 AM
    • Bangalore, India
    • Posts 258

    Directly this is not supported in MVC,

    But you can do something like the one which is given in the following post

    http://geekswithblogs.net/AzamSharp/archive/2007/12/13/117684.aspx

    Please remember to click “Mark as Answer” on the post that helps you

    Best Regards
    Brij Mohan
    http://www.dotnetglobe.com
  • Re: How to pass data from view to controller Mvc preview 5

    10-14-2008, 9:33 AM
    Answer

     Avani,

     I don't think you can use Webform controls as a rule.  You can use either straight HTML controls or form helpers to generate the page controls.  After that, you'll need code both in your controller action & in the view page.  This code was taken from a working example.

     It is also possible to cast ViewData so it is strongly typed.  There are several blog postings on the Internet illustrating how to do it if you're interested.

    <%@import Namespace="Relations" %>
    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
        <h2><%= Html.Encode(ViewData["Message"]) %></h2>
        <ul  id="childs">
        <% foreach (Parent parent in ViewData["Parents"] as IEnumerable<Parent>)
           {
             %>
             <li><%= parent.birthDate%>-<%= parent.birthPlace%>
                <div>
                    <table width="90%" cellpadding="0px;" cellspacing="0px;">
                        <%
                        foreach (Child child in parent.Children)
                        {                         
                        %>
                             <tr><td><%= child.firstName%></td><td><%= child.lastName%></td><td><%= child.birthDate%></td></tr>  
                        <%
                        }
                        %>
                    </table>
                    <a href="/Family/Show/<%= parent.RID%>">View Family</a><br /><br /><br />
                </div>
             </li>
        <%} %>
        </ul>
    </asp:Content>

     namespace Relations.Controllers
    {
        
        public class FamilyController : Controller
        {
            [Authorize]
            public ActionResult Show()
            {
                RelationsDataContext relations = new RelationsDataContext();
                IEnumerable<Parent> parentViewData = (from p in relations.Parents
                              orderby p.birthDate descending
                              select p);

                ViewData["Parents"] = parentViewData;

                return View();
            }
    }

     

Page 1 of 1 (3 items)