Reputation: 27
I'm trying to create a web application using ASP.NET MVC (though I'm still a beginner so I don't know much about C#, I only used VB.net). I want my ASP.NET MVC application to store the content of the HTML input tag like username and password to sign in to my application but it didn't work out.
(PS : I'm still a beginner so please make it as simple as it can get)
I already tried many tutorials before coming here, but none of them seems to work. Here's is the code I tried
// in the Model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MoviesApp.Models
{
public class Movie
{
public string id { get; set; }
public string name { get; set; }
}
}
// in the controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MoviesApp.Models;
namespace MoviesApp.Controllers
{
public class MoviesController : Controller
{
// GET: Movies
public ActionResult Random(string movieid, string moviename)
{
var movie = new Movie();
movie.id = movieid;
movie.name = moviename;
return View(movie);
}
}
}
//in views :
@model MoviesApp.Models.Movie
@{
ViewBag.Title = "Random";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<form method="post" action="Random">
<input type="text" name="movieid"/>
<input type="text" name="moviename"/>
<input type="submit" value="save" />
</form>
Upvotes: 0
Views: 409
Reputation: 8912
The default MVC controller route
defined in App_Start/WebApiConfig.cs
is
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Hence, call controller method as following
https://localhost:44397/Movies/Random?movieid=Shrek&moviename=first
Upvotes: 1
Reputation: 27
I think it worked since my link changed to : https://localhost:44397/Movies/Random?movieid=Shrek&moviename=first
but my method is now get and the attribute is httpget insted of httppost would you please explain what post and get are and when should i use post or get ?
PS: when i added httppost to the controller and set the method to post and the action to /MoviesController/Random it gave me the error ressouce cannot be found
Upvotes: 0
Reputation: 38
I think action should look like this. And [HttpPost]
in the controller
<form method="post" action="/Movies/Random">
Upvotes: 1
Reputation: 11788
Please try adding this to your controller method (you use POST instead of GET in your client side code):
[HttpPost]
public ActionResult Random(string movieid, string moviename)
{
var movie = new Movie();
movie.id = movieid;
movie.name = moviename;
return View(movie);
}
Upvotes: 1