Newbie - what is the purpose of attributes?

Z

Zalek Bloom

I read about attributes - it says it has to do something with
metadata.
Now I am testing a sample code MVC3 from the Internet:

public ActionResult Create()
{
return View();
}

[HttpPost]
public ActionResult Create(Movie newMovie)
{
if (ModelState.IsValid)
{
db.Movies.Add(newMovie);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(newMovie);
}
}

When I commented the statement [HttpPost] and I run my application
(from the site:
http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part6-cs

I am getting:

Server Error in '/' Application.
The current request for action 'Create' on controller type
'MoviesController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type
Mvc3Movie.Controllers.MoviesController
System.Web.Mvc.ActionResult Create(Mvc3Movie.Models.Movie) on type
Mvc3Movie.Controllers.MoviesController
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

So this is my question:
What [HttpPost] is doing?

Thanks,

Zalek
 
A

Arne Vajhøj

I read about attributes - it says it has to do something with
metadata.
Now I am testing a sample code MVC3 from the Internet:

public ActionResult Create()
{
return View();
}

[HttpPost]
public ActionResult Create(Movie newMovie)
{
if (ModelState.IsValid)
{
db.Movies.Add(newMovie);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
return View(newMovie);
}
}

When I commented the statement [HttpPost] and I run my application
(from the site:
http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part6-cs

I am getting:

Server Error in '/' Application.
The current request for action 'Create' on controller type
'MoviesController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type
Mvc3Movie.Controllers.MoviesController
System.Web.Mvc.ActionResult Create(Mvc3Movie.Models.Movie) on type
Mvc3Movie.Controllers.MoviesController
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

So this is my question:
What [HttpPost] is doing?

Attributes are directives to something loading, examining and
executing the code via reflection.

http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattribute.aspx

explains what [HttpPost] does.

Arne
 
Z

Zalek Bloom

I read about attributes - it says it has to do something with
metadata.
Now I am testing a sample code MVC3 from the Internet:
public ActionResult Create()
         {
             return View();
         }
        [HttpPost]
         public ActionResult Create(Movie newMovie)
         {
             if (ModelState.IsValid)
             {
                 db.Movies.Add(newMovie);
                 db.SaveChanges();
                 return RedirectToAction("Index");
             }
             else
             {
                 return View(newMovie);
             }
         }
When I commented the statement [HttpPost] and I run my application
(from the site:
http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part6-cs
I am getting:
Server Error in '/' Application.
The current request for action 'Create' on controller type
'MoviesController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type
Mvc3Movie.Controllers.MoviesController
System.Web.Mvc.ActionResult Create(Mvc3Movie.Models.Movie) on type
Mvc3Movie.Controllers.MoviesController
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
So this is my question:
What [HttpPost] is doing?

Attributes are directives to something loading, examining and
executing the code via reflection.

http://msdn.microsoft.com/en-us/library/system.web.mvc.httppostattrib...

explains what [HttpPost] does.

Arne

Thanks!!!!

Zalek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top