0

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>

4 Answers 4

1

I think action should look like this. And [HttpPost] in the controller

<form method="post" action="/Movies/Random">
Sign up to request clarification or add additional context in comments.

4 Comments

my first answer is wrong. the true is this:<form method="post" action="/Movies/Random">. /MoviesController/Random is wrong, true is "/Movies/Random".
I tried both but when i use post it gives me the ressource cannot be found error but when i use get it works but it adds the result to the link can you please help me ?
I've tried both the way I suggested to you, both by typing "Random" in Action and without [HttpPost] as you do. both worked
Okey I'll restart everything from the begining and see maybe i made a mistake thank you for the help.
1

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);
}

1 Comment

I added this HttpPost attribute and i left the method as post and when i run the application it gave me resource not found. but when i changed both the method and http atrribute to get i thin it worked because it gave me this link:localhost:44397/Movies/Random?movieid=Shrek&moviename=first
1

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

Get vs Post

3 Comments

So should i make a custom route or change this default one ?
Two ways of routing 1) Conventional routing (as given above) 2) Attribute routing. You continue with Conventional routing but make sure you're sending request as per route defined.
Sorry i missed, you're sending request to MVC controller and not API Controller. I updated my answer. Also in your route, you should not use word 'Controller'.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.