Using ASP.NET MVC4 (.NET 4, C#) on MSVC 2010 SP1, I've noticed that I can pass a class model to a view using Razor and display the model using DisplayModelFor & EditorForModel but when I try to do the same using the ASPX view engine it doesn't work. Why is that? Code snippets from my test project below. Thanks.
My Controller:
namespace MvcApplication1.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
TestModelClass c = new TestModelClass();
c.MyInt = 999;
return View(c);
}
}
My Model:
namespace MvcApplication1.Models
{
public class TestModelClass
{
public int MyInt { get; set; }
}
}
My View:
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.TestModelClass>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<%Html.DisplayForModel(); %>
</div>
</body>
</html>
Alternate Razor (Works):
#model MvcApplication1.Models.TestModelClass
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#Html.DisplayForModel()
Successful output from Razor version:
Index
MyInt
999
You are missing a :.
The correct syntax should be
<%: Html.DisplayForModel() %>
Related
I would like to use ViewBag values in the shared view of layout but provided from another controller. In particular, I would like to render the principal action and then add few variables, coming from another controller, that are used in a lot of pages (but not in all pages to justify the use of global variables).
Views/Shared/_Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8" />
<title>#ViewBag.Title</title>
#Styles.Render("~/Content/css_custom")
#Scripts.Render("~/bundles/modernizr")
</head>
<body>
#Html.Action("myAction", "myController")
<p>Value: #ViewBag.testval </p>
#RenderBody()
</body>
</html>
Controllers/myController.cs
namespace myProject.Controllers
{
public class myController : Controller
{
public void myAction() {
string test_value = "Hey!";
ViewBag.testval = test_value;
}
}
}
In the layout the only Viewbag variables that I can access are the ones of the action target of RenderBody.
You should return that view.
public ActionResult Index()
{
ViewBag.testval = "Test value";
return View();
}
im just start learning asp.net and encounter this error
i've tried many solution from google but still get this error (errr...)
Compiler Error Message: CS1061: 'Exercise1.Visitor' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'Exercise1.Visitor' could be found (are you missing a using directive or an assembly reference?)
im using VS2012 with 4.5 Net
this is my code
Model
namespace Exercise1.Models
{
public class Visitor
{
public String Name
{
set;
get;
}
}
}
Controller
namespace Exercise1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Visitor data)
{
ViewBag.Message= "Hi my name is" + data.Name;
return View();
}
}
}
View
#{
Layout = null;
}
#model Exercise1.Visitor
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>
#using(Html.BeginForm())
{
<p>
#Html.LabelFor(m=>m.Name);
</p>
}
</div>
</body>
</html>
btw, this is my first question hehe
Change model to:
#using Exercise1.Models
#model Exercise1.Models.Visitor
Do you have another Vistor class in your project? it's saying Exercise1.Visitor when it should be complaining about Exercise1.Models.Visitor
make sure you're referencing Exercise1.Models.Visitor
I create Spring MVC project like this.
This is Controller:
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public String doHello(Model model) {
model.addAttribute("message", "Hello Spring MVC");
return "helloworld";
}
}
And this is content of file "helloworld.jsp".
<body> <h1>${message }</h1> </body> // I don't know why I can't post full code
When i run URL "http://localhost:8080/HelloSpringMVC/hello", the result should be Hello Spring MVC but I got ${message }, what's going on?
Make sure you have javax expression language dependency in your project.
add <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> to jsp file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello ${message }
</h1>
</body>
</html>
try this:
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView doHello() {
ModelAndView model = new ModelAndView("helloworld");
model.addObject("message", "Hello Spring MVC");
return model;
}
}
I am new to MVC and creating a sample application by viewing this video.I have created a class in Model and a controller.Now when i try to access Class property through view,i am not able to access it,even when i am writing in view like <%=Model.Id %>,the angular brackets are not yellow marked as in case of server side scripting.I am not able to find where the problem is?
Class Customer in Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcCustomer.Models
{
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public double Amount { get; set; }
}
}
and Controller is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcCustomer.Models;
namespace MvcCustomer.Controllers
{
public class LoadCustomerAndDisplayController : Controller
{
//
// GET: /LoadCustomerAndDisplay/
public ActionResult Index()
{
Customer customer = new Customer();
customer.Name = "Devesh";
customer.Amount = 22.9;
customer.Id = 1;
return View(customer);
}
}
}
and my strongly typed view(IntelliSense is also not working) is
#model MvcCustomer.Models.Customer
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
</head>
<body>
<div>
The Customer id is: <%= Model.Id %>
</div>
</body>
</html>
First off, you're using a Razor view, not an ASP.NET view, so the syntax is #Model.Id not <%= Model.Id %>.
Your view is coded with Razor.
To initiate server side behavior use #, not <%= ... %>
Have a look here.
http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
I know that it is possible to define custom tags in ASP.NET with User Controls. But as far as I know you can only add attributes to these controls. I would like to be able to embed more complex data, a bit lite this:
<myControls:MyGraph id="myGraph1" runat="server">
<colors>
<color>#abcdef</color>
<color>#123456</color>
</colors>
</myControls:MyGraph>
It this possible in ASP.NET? Should I try to extend a ListView? Or it there a better and more correct solution?
It is certainly possible. For your example the classes would look like:
[ParseChildren(true)]
class MyGraph : WebControl {
List<Color> _colors = new List<Color>();
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<Color> Colors {
get { return _colors; }
}
}
class Color {
public string Value { get; set; }
}
And the actual markup would be:
<myControls:MyGraph id="myGraph1" runat="server">
<Colors>
<myControls:Color Value="#abcdef" />
<myControls:Color Value="#123456" />
</Colors>
</myControls:MyGraph>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
namespace ComponentDemo
{
[ParseChildren( true )]
public class MyGraph : System.Web.UI.WebControls.WebControl
{
private List<Color> _colors;
public MyGraph() : base() { ;}
[PersistenceMode( PersistenceMode.InnerProperty )]
public List<Color> Colors
{
get
{
if ( null == this._colors ) { this._colors = new List<Color>(); }
return _colors;
}
}
}
public class Color
{
public Color() : base() { ;}
public string Value { get; set; }
}
}
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ComponentDemo._Default" %>
<%# Register Assembly="ComponentDemo" Namespace="ComponentDemo" TagPrefix="my" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<my:MyGraph runat="server">
<Colors>
<my:Color Value="value1" />
<my:Color Value="value2" />
<my:Color Value="value3" />
<my:Color Value="value4" />
</Colors>
</my:MyGraph>
</div>
</form>
</body>
</html>
You cannot user UserControl for such purpoces. As adviced above, inherit Control or WebControl.