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.
Related
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
I am following the NerdDinner part in the Book Professional ASP.NET MVC 2. Currently i am at the part where i need to implement the DinnerFormViewModel and the Renderpartial Dinnerform.
The book contains some errors here so I tried to search on the internet and fix it myself..
I have put the DinnerFormViewModel in the Models folder this is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace NerdDinner.Models
{
public class DinnerFormViewModel : Controller
{
private static string[] _countries = new[]{
"USA",
"Ireland",
"Scotland",
"Namibia"
};
//Properties
public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }
//Constructor
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
Countries = new SelectList(_countries, dinner.Country);
}
// GET: /DinnerFormViewModel/
public ActionResult Index()
{
return View();
}
}
}
Then i have made the DinnerForm.ascx (Partial class):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<%: Html.LabelFor(m => m.Dinner.Title) %>
<%: Html.TextBoxFor(m => m.Dinner.Title) %>
<%: Html.ValidationMessageFor(m => m.Dinner.Title, "*") %>
ETC...
and i have made the edit.aspx as follows:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Edit: <%: Model.Dinner.Title %>
</asp:Content>
<asp:Content ID="Edit" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>
Now if i start the application, an error at <% Html.RenderPartial("DinnerForm"); %> will popup saying:
c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c8cca855\23406a1e\App_Web_dinnerform.ascx.32d6c807.tczxq3bd.0.cs(166): error CS0030: Cannot convert type 'ASP.views_dinners_dinnerform_ascx' to 'System.Web.Mvc.ViewUserControl'
I think it has something to do with the namespaces, but i can't fix the error, someone faced the same problem or someone here that can help me out?? Thank you!:)
Your partial view should inherit from System.Web.Mvc.ViewUserControl.
ViewPage is for a full view.
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>>" %>
I'm currently working my way through the MVC Music Store tutorial. I'm stuck on page 53 at the moment and I was wondering if someone could help me out.
I'm currently receiving the following two errors:
'object' does not contain a definition for 'Artists'
'object' does not contain a definition
for 'Genres'
I think I've looked at it for too long now and I can't spot my error. A fresh pair of eyes may do the trick!
Here is the aspx file in which the error occurs:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMovies1.ViewModels.StoreManagerViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<% using (Html.BeginForm())
{ %>
<%: Html.ValidationSummary(true)%>
<fieldset>
<legend>Edit Album</legend>
<%: Html.EditorFor(model => model.Album,
new object{ Artists = Model.Artists, Genres = Model.Genres }) %>
<p><input type="submit" value="Save" /></p>
</fieldset>
<% } %>
</asp:Content>
And here is the class that this page should be calling from, so to speak:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcMovies1.Models;
namespace MvcMovies1.ViewModels
{
public class StoreManagerViewModel
{
public Album Album { get; set; }
public List<Artist> Artists { get; set; }
public List<Genre> Genres { get; set; }
}
}
PS - I realise some of my names are MvcMovies1, I called it that by mistake but everything is referenced accordingly.
Replace:
new object{ Artists = Model.Artists, Genres = Model.Genres }
With:
new { Artists = Model.Artists, Genres = Model.Genres }
In other words, you want to create a new anonymous type. Your syntax above actually attempts to create a new bare object and assign values to the nonexistent properties on object: "Artists" and "Genres".
Is it possible to create a "generic" pager (ASCX UserControl) which can be used with different grids on different pages to control paging? So that I only need to render it using RenderPartial.
I'm currently working on a "Contacts" grid, which needs paging functionality, but I will have to reuse paging later, so I wondered if I can make this a shared partial control.
One issue I can think of right now though: How do I control the AJAX link when clicking on any of the pager numbers, as that needs to be different if it is another grid?
You could create a PagerViewModel object, with the following properties:
public PagerViewModel
{
public string Controller { get; set; }
public int StartPage { get; set; }
public int EndPage { get; set; }
}
And create a Pager.ascx partial view as follows:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PagerViewModel>" %>
<% for(int i = Model.StartPage; i <= Model.EndPage; i++) { %>
<%= i %>
<% } %>
I think you can figure out how to use it ;-)
Look at MVCContrib: http://mvccontrib.codeplex.com/Wiki/View.aspx?title=Documentation
Here is example: http://www.c-sharpcorner.com/Blogs/BlogDetail.aspx?BlogId=875 of using generic Pager.