This is driving me crazy. I'm not including any code because I want to know the best approach to do this conceptually without influencing the answer.
In Asp.Net MVC 2 (beta 2010 incidently) I have a MasterPage Site.Master with a UserControl "SignIn". This user control has 3 fields that I want to get data from. The TextBox value "Email", the TextBox value "Password" and the bool CheckBox value "RememberMe". Now I have tried a number of things but all of them relate to the action being passed to the HomePageController which has its own Model type which is not the same as the UserControl's Model type. UserControl Model = User class, HomePageController Model = specialized HomePageData Model which has things I need to populate my HomePage View Content.
I can wire into the HomePageController Action "SignIn" both via ActionLink and a specific Post with parameters (string Email, string Password, bool? RememberMe) but no values of my HtmlHelper controls residing on the UserControl SignIn.ascx ever get passed into the conroller seemingly no matter what I try.
The SignIn control truly needs to reside on the Site.Master but I'm not sure I'm doing the right thing by using the HomePageController vs. setting up an "AccountController" but the point is moot unless I can somehow get the values entered by the user into the 3 input fields of the UserControl.
To note: I have tried UpdateModel (which does not throw an exception but all of the fields of the user object are null, TryUpdateModel with similar results, TryUpdateModel returns true but all null properties on the user instance, [AcceptVerbs["Post"]] in a HomePageController method "SignIn" with and without parameters, again no values passed or able to be retrieved from the UserControl correctly. ad nauseam.)
I have scoured the internet and cannot find an example accomplishing exactly what I'm trying to do with the MasterPage->UserControl->XyzController scenario (again, I'm not insistent on using HomePageController for this so the proper way to do this may use a "viewless" controller.
Somebody give me a Merry Christmas (if anyone's working lol)
Thanks in advance... oh and if you only know Vb.Net that's fine as well, I'll convert your answer to C#.
OK - I tried the suggestions below to no avail. I created a simple example (without the suggestions being used as a base to start from)
my MasterPage (Site.Master) is as follows...
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<% Html.RenderPartial("SignIn"); %>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</body>
</html>
and my SignIn UserControl is as follows
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<StackOverflowExample.Models.WebUser>" %>
Email: <%= Html.TextBox("Email") %>
Password: <%= Html.TextBox("Password") %>
<%= Html.ActionLink("Sign In", "SignIn", "Home") %>
...and my simplified Model is as follows ....
namespace StackOverflowExample.Models
{
public class WebUser
{
public string Email { get; set; }
public string Password { get; set; }
}
}
... and my HomeController is as follows ...
namespace StackOverflowExample.Controllers
{
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult SignIn()
{
var user = new WebUser();
try
{
UpdateModel(user);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
throw;
}
return Redirect("Index");
}
}
}
... from this simple example, if I can get data into the WebUser instance inside HomeController, or any other Controller for that matter I can use the posted example to fix my real-world website. I know I'm missing something very fundamental here and I'm 99% sure is has to do with the fact that the SignIn control is strongly-typed to the WebUser class but the HomeController or HomeView (Index.aspx) is not strongly typed to the same (or any other) Model.
Here's the skimpy Index.aspx Home View (just in case anyone needs it ...)
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>