Pass Generic Model to View - c#

I have this in my Page setup;
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel>" %>
And the interface looks like this;
public interface ILocationsFormViewModel<T>
{
List<TopLocation<T>> Locations { get; set; }
List<TrendingItem<ITrendItem>> TrendingLocations { get; set; }
}
T is either ITopHotel or ITopShop etc etc with no properties
The problem is I get this error when I goto the View;
CS0305: Using the generic type 'ILocationsFormViewModel' requires
'1' type arguments
Does anyone know how I can pass a class that has a Generic type to a view?

You probably need the generic type parameter in there:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel<T>>" %>

Related

How to put a class in aspx file?

I'm brand new to asp.net. I added a class to my aspx file and now it does not compile:
Compiler Error Message: CS1513: } expected
Line 206: }
Line 207:
Line 208: private void #__Render__control1(System.Web.UI.HtmlTextWriter #__w, System.Web.UI.Control parameterContainer) {
Line 209:
Line 210: #line 7 "c:\aspx\dummy.aspx"
I'm not even using the class yet. The class is simple enough:
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
I'm not using visual studio, just a text editor, so no replies with solutions that involve VS please.
I got the entire file down to this:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Text" %>
<%# Import Namespace="System.Xml" %>
<%# Import Namespace="System.Data" %>
<%
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
%>
Works without the class. Can somebody point me in the right direction please.
To declare a class, you need to put it in a <script> tags:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Text" %>
<%# Import Namespace="System.Xml" %>
<%# Import Namespace="System.Data" %>
<script type="text/c#" runat="server">
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
</script>
<%
// Create an instance of the class declared above
var a = new AddressSummary();
%>

I am writting an mvc application and having an error when trying to get a loop of products using strongly-typed

I created a strongly typed view which view data class is the class called ProductList. I am returning to the view a list object as well that comes from getting all the products by using linq to entitites. Why am I getting an error when trying to use the product.name as shown on the code below on the view. Im very new on this so sorry if my question is bad. Any help would be very appreciated
public class ProductList
{
public IList<Product> Products { get; set; }
}
this is the controller :
public ViewResult List()
{
IQueryable<Product> allproducts= productrepository.selectAll();
return View(allproducts.ToList());
}
and this is the view :
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"Inherits="System.Web.Mvc.ViewPage<TradeIt.Models.ProductListViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
List
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var product in Model.Products)
{ %>
<%: Html.LabelFor(product.Name) %>
<% } %>
</asp:Content>
I am getting the next error: The type argument for method 'system.web.mvc.html.labelExtensions.LabelFor... cannot be inferred from the usage . Try specifiying the type arguments explicitly'
change this
"Inherits="System.Web.Mvc.ViewPage" %>
your model is called ProductList, not ProductListViewModel

Inlining a Public Variable in a User Control

I have a user control with a variable that I am trying to display inline in the Aspx page, but it always shows as empty. I've done this in other user controls and I'm not sure why it isn't working.
Aspx...
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyClass.ascx.cs" Inherits="MyClass" %>
<%# sMyPublicString %>
Code Behind...
public partial class MYClass : System.Web.UI.UserControl
{
public string sMyPublicString = "MyValue";
}
In parent webform on Page_Load you have to add Page.DataBind()

Advice on how to create a common base class (or functionality) between a page and master?

I created a custom base class that my .aspx pages inherit from.
Since Master page's inherit from MasterPage and not Page, how can I create common functionality that are available in both my Pages and Master pages?
public class SitePage : System.Web.UI.Page
{
public SitePage()
{
}
public bool IsLoggedIn
{
//
}
public string HtmlTitle
{
//
}
}
One approach would be to put all the functionality in the Master page and always call them by going through the Master page. You can strongly type the Master property in SitePage:
public class SitePage : Page
{
public new MyMaster Master { get { return base.Master as MyMaster; } }
}
Then access the values through the Master:
this.Master.IsLoggedIn
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
Inherits="MyProject.MasterPages.MyProject" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Base page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyProject.Master"
AutoEventWireup="true" CodeBehind="BasePage.aspx.cs"
Inherits="MyProject.BasePage" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>
Content Page:
<%# Page Title="MyProject - Home" Language="C#"
MasterPageFile="~/MasterPages/MyProject.Master" AutoEventWireup="true"
CodeFileBaseClass="MyProject.BasePage" CodeFile="Default.aspx.cs"
Inherits="MyProject.Default"
Meta_Description="Code Snippet: Master Page and Base Page"
Meta_Keywords="master, base, content" Theme="Style" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>

how to make a web user control in MVC?

i made an easy web user control by reading Scottgu articles
BUT; my user control return to me error:
CountryDropDown.ascx:
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%=ViewData["countries"] = new string[] { "France", "Germany", "US", "UK" })%>
<%var q = ViewData["countries"]; %>
<%= Html.DropDownList("",ViewData["countries"] as SelectList)%>
MY VİEW : <%= Html.EditorFor(c=>c.Country,"CountryDropDown") %>
MODEL:
public class Customer
{
[Required(ErrorMessage="NameRequired")]
[StringLength(50,ErrorMessage="Must be less than 50")]
public string Name { get; set; }
[Range(1,20,ErrorMessage="Invalid Age")]
public int Age { get; set; }
[Required(ErrorMessage="Email Required")]
[RegularExpression(#"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}")]
public string Email { get; set; }
[UIHint("CountryDropDown")]
public string Country { get; set; }
}
how to make a ASCX in mvc with dropdownlist?
Using <%= tells ASP to print the following statement. You don't want to be doing that in this case.
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<%
ViewData["countries"] = new string[] { "France", "Germany", "US", "UK" };
var q = ViewData["countries"];
%>
<%= Html.DropDownList("",ViewData["countries"] as SelectList)%>
Try that.
Also, why do you assign q and not use it?
Change
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
to
<%# Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Customer>" %>
Assuming Customer is in the root of your namespace.
General remarks, I'm assuming that you are learning MVC and you want to use latest version. Article that you have referred to is for MVC2. I suggest you work through MVC 3 examples on here:
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part1-cs
Razor syntax should make your life a lot easier. You are also referring to controls (your previous posts). Controls are present in web forms and they are great for when you want to develop components.
MVC is quite different in that sense and you should get a better understanding once you work through the examples.

Categories

Resources