NerdDinner DinnerFormViewModel Error CS0030 error - c#

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.

Related

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

How to send two objects from a form in asp.net c# in an aspx page

I need to send two objects to my controller using bindings but I have no idea how to do it. I just know how to send one object not multiple objects.
This is an example of how I'm doing it with a singular object.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<Inventario.Models.Report>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create</h2>
<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Report</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Client, "Client_1") %>
</div>
<div class="editor-field">
<%: Html.DropDownList("Client", String.Empty) %>
<%: Html.ValidationMessageFor(model => model.Client) %>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
Then, It is received by the controller this way:
[HttpPost]
public ActionResult Create(Report report)
{
//Some code here
}
And what I need is to get two objects. IDK maybe something like this:
[HttpPost]
public ActionResult Create(Report report, AnotherObject ao)
{
//Some code here
}
I'm a beginer on this. Any guide you can give me will help me a lot. Thanks by advance.
You have to create a ViewModel like this:
public class MyViewModel
{
public Report report {get;set;}
pulic AnotherObject ao {get;set;}
}
From you Get Action pass them:
public ActionResult Create()
{
MyViewModel model = new MyViewModel();
model.report = new Report();
model.ao = new AnotherObject();
return View(model);
}
In your view set Model to MyViewModel
and post it in action:
[HttpPost]
public ActionResult Create(MyViewModel myModel)
{
//Some code here
}

Why doesn't my checkbox map to an MVC model member?

I'm trying to implement what this answer suggests but without storing the display name in my model code, so I believe it's a separate question.
I have an MVC view
<%# Page Language="C#" MasterPageFile="PathToMaster" Inherits="System.Web.Mvc.ViewPage<ModelData>" %>
and a model:
public class ModelData {
public bool Option1 { get; set; }
}
and I have a .aspx with HTML markup for a form that contains a checkbox:
<label for="MyCheckbox">Your choice</label>
<%= Html.CheckBoxFor(Model=>Model.Option1, new { id="Option1", #class="checkbox", onchange="return myValidation();", name="MyCheckbox", value="Option one" } ) %>
which when it is compiled yields this HTML:
<label for="MyCheckbox">Your choice</label>
<input class="checkbox" id="Option1" name="Option1" onchange="return myValidation();" type="checkbox" value="Option one" /><input name="Option1" type="hidden" value="false" />
and whenever I check the checkbox and submit the form the controller-action
class MyController : Controller {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult RequestStuff( ModelData data )
{
}
}
receives data where Option1 is false.
What am I doing wrong? How do I make the checkbox map to the model member?
Since you said changing the alias in the helper expression didn't cut it, I took the time to prepare the MVC2 test project, here's how it looks:
MyModel.cs:
namespace TestAppMVC2.Models
{
public class MyModel
{
public bool Option1 { get; set; }
}
}
Controller's actions:
public ActionResult MyAction()
{
var viewModel = new MyModel();
return View(viewModel);
}
[HttpPost]
public ActionResult MyAction(MyModel viewModel)
{
bool option = viewModel.Option1;
return View();
}
The view (MyAction.aspx):
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TestAppMVC2.Models.MyModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
MyAction
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>MyAction</h2>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Option1) %>
</div>
<div class="editor-field">
<%: Html.CheckBoxFor(model => model.Option1) %>
<%: Html.ValidationMessageFor(model => model.Option1) %>
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
Looks like this:
And the result after checking the checkbox:
By the way, the View was generated entirely (well, almost entirely, I changed TextBoxFor to CheckBoxFor) by Add View -> Strongly Typed View for the MyModel class and template: Edit.
So it looks like the binding above should be OK. Try this and then you can fiddle with custom options on checkboxes. :)
Like I wrote in the other answer, the idea is essentially the same, only in MVC3 the Razor syntax looks a bit cleaner. But that's about it when it comes to differences. :)
In your line:
<%= Html.CheckBoxFor(Model=>Model.Option1, new { id="Option1", #class="checkbox", onchange="return myValidation();", name="MyCheckbox", value="Option one" } ) %>
Replace Model with another alias, i.e. m. So it looks like:
<%= Html.CheckBoxFor(m=>m.Option1, new { id="Option1", #class="checkbox", onchange="return myValidation();", name="MyCheckbox", value="Option one" } ) %>
You referred to the Model directly where CheckBoxFor takes an an HTML helper instance.
I believe the following maybe appropriate to help you understand further:
MVC HTML Helpers and Lambda Expressions

Converting IQueryable to list and iterating using foreach

I am a noob to mvc.I am using a repository pattern with linq2sql using mvc
Just doing some test to get a clear idea everything around the it.
I am trying to output authors from author table.
public class AuthorsRepository : IAuthorRepository
{
private Table<BK_Author> _authorsTable;
public IQueryable<BK_Author> Authors
{
get { return _authorsTable.AsQueryable<BK_Author>(); }
}
in the controllers
public class AuthorsController : Controller
{
private IAuthorRepository _authorRepo;
public AuthorsController()
{
string connectionString = "";
_authorRepo = new AuthorsRepository(connectionString);
}
public ViewResult List()
{
return View(_authorRepo.Authors.ToList());
}
on the view part
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
<div class="item">
<% foreach (var k in Model)
{ %>
<%: k.Author_Name %>
<%: k.Author_email %>
<%: k.Author_phonenumber %>
<%: k.Author_Website %>
<% }%>
</div>
When I try to run it I am having a compiler exception
"foreach statement cannot operate on variables of type 'BK_Author' because 'BK_Author' does not contain a public definition for 'GetEnumerator'"
Page's model is somehow set to an object of BK_Author and not of AuthorsRepository. Set it to AuthorsRepository and access list of authors using its Authors property.
In your View page, you could simply inherit something like this:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<Authors>>"
or after foreach(var k in Model) you should case k to Authors like:
Authors author = (Authors)k;
Hope this help!!

MVC Error: 'object' does not contain a definition for ''

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".

Categories

Resources