locked
Actions in controller RRS feed

  • Question

  • User1735445714 posted

    yes this is the code i am trying for the Insert Action:

    public class myController : Controller
    {
    // GET: my
    public ActionResult Index()
    {
    return View();
    }
    [HttpPost]
    public ActionResult Index(Customer_Registration cg)
    {

    string mainconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(mainconn);
    string sqlquery = "Insert into [dbo].[Customer_Registration] ([Username],[Age],[Email],[Your_Password],[Confirm_Password],[Gender]) values (@Username,@Age,@Email,@Your_Password,@Confirm_Password,@Gender)";
    SqlCommand sqlcomm = new SqlCommand(sqlquery,sqlconn);
    sqlconn.Open();
    sqlcomm.Parameters.AddWithValue("@Username",cg.Username);
    sqlcomm.Parameters.AddWithValue("@Age",cg.Age);
    sqlcomm.Parameters.AddWithValue("@Email",cg.Email);
    sqlcomm.Parameters.AddWithValue("@Your_Password", cg.Your_Password);
    sqlcomm.Parameters.AddWithValue("@Confirm_Password",cg.Confirm_Password);
    sqlcomm.Parameters.AddWithValue("@Gender",cg.Gender);
    sqlcomm.ExecuteNonQuery();
    ViewBag.SuccessMessage = "Successfully Created!";
    // ViewData["Message"] = "Created Successfully";
    sqlconn.Close();
    return View();
    }
    [HttpPost]
    public ActionResult Insert(Product pr)
    {

    return View();
    }
    }
    }

    Tuesday, June 16, 2020 4:14 PM

All replies

  • User475983607 posted

    Do you have a question?

    Tuesday, June 16, 2020 4:17 PM
  • User1735445714 posted

    yes

    Tuesday, June 16, 2020 4:25 PM
  • User1120430333 posted

    yes this is the code i am trying for the Insert Action:

    https://en.wikipedia.org/wiki/Separation_of_concerns#Subject-oriented_programming

    https://www.c-sharpcorner.com/UploadFile/56fb14/understanding-separation-of-concern-and-Asp-Net-mvc/

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs

    <copied>

    An MVC model contains all of your application logic that is not contained in a view or a controller. The model should contain all of your application business logic, validation logic, and database access logic. 

    A view should contain only logic related to generating the user interface. A controller should only contain the bare minimum of logic required to return the right view or redirect the user to another action (flow control). Everything else should be contained in the model.

    In general, you should strive for fat models and skinny controllers. Your controller methods should contain only a few lines of code. If a controller action gets too fat, then you should consider moving the logic out to a new class in the Models folder.

    <end>

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    example of you have more code in one action method than there is in the entire example controller,

    using System.Web.Mvc;
    using MVC.Models;
    
    namespace MVC.Controllers
    {
        [Authorize]
        public class StudentsController : BaseController  
        {
            private IStudentModels studmods;
            public StudentsController(IStudentModels studentModels)
            {
                studmods = studentModels;
            }
    
            // GET: Students
    
            [AllowAnonymous]
            public ActionResult Index()
            {
                return View(studmods.GetStudents());
            }
    
            //[AllowAnonymous]
            public ActionResult Details(int id = 0)
            {
                return id == 0 ? null : View(studmods.GetStudentById(id));
            }
            public ActionResult Create()
            {
                return View(studmods.Create());
            }
    
            [HttpPost]
            public ActionResult Create(StudentViewModels.Student student)
            {
                if (ModelState.IsValid)
                {
                    studmods.Create(student);
                    return RedirectToAction("Index");
                }
    
                return View(student);
            }
    
            public ActionResult Edit(int id = 0)
            {
                return id == 0 ? null : View(studmods.Edit(id));
            }
    
            [HttpPost]
            public ActionResult Edit(StudentViewModels.Student student)
            {
                if (ModelState.IsValid)
                {
                    studmods.Edit(student);
                    return RedirectToAction("Index");
                }
    
                return View(student);
            }
    
            public ActionResult Delete(int id = 0 )
            {
                if (id > 0) studmods.Delete(id);
                
                return RedirectToAction("Index");
             }
        }
    }

    Tuesday, June 16, 2020 4:27 PM
  • User1735445714 posted

    there is one action method by name of Index it has a view page with name index and has a form which inserts data into table. and at bottom of code there is another action method with insert name I want to create another view and it also has an insertion form to add data into other table, but it is not working in the same controller

    Tuesday, June 16, 2020 4:31 PM
  • User475983607 posted

    there is one action method by name of Index it has a view page with name index and has a form which inserts data into table. and at bottom of code there is another action method with insert name I want to create another view and it also has an insertion form to add data into other table, but it is not working in the same controller

    What does "not working" mean!!!

    Are you trying to create a new View but do not know how?  The simplest way to create a View is to right click the Action and select "Add View", then follow the wizard.  

    Tuesday, June 16, 2020 4:33 PM
  • User1735445714 posted

    when i run the form page in chrome it says not found

    Tuesday, June 16, 2020 4:36 PM
  • User475983607 posted

    Masoud Abedi

    when i run the form page in chrome it says not found

    Is the URL correct?  Your controller is named "my".  Therefore the html form action URL would be...

    /my/Insert

    What URL are you using in the HTML form? 

    If you are trying to access the URL from the Browser then of course it does not work.  You did not create a GET Insert Action. 

    Edit: Example

    [HttpGet]
    public ActionResult Insert()
    {
        return View();
    }

    Tuesday, June 16, 2020 4:45 PM
  • User1735445714 posted

    it is localhost.

    http://localhost:1923/my/Insert

    Tuesday, June 16, 2020 4:48 PM
  • User475983607 posted

    You forgot the HttpGet Action.

    [HttpGet]
    public ActionResult Insert()
    {
        return View();
    }

    Tuesday, June 16, 2020 4:49 PM
  • User1735445714 posted

    i should use exactly the same?

    the get method?

    Tuesday, June 16, 2020 4:51 PM
  • User475983607 posted

    i should use exactly the same?

    the get method?

    IMHO, you should go through the Getting Started tutorial so you understand the fundamentals rather than guessing. 

    But yes you need an HttpGet Insert Action.  The browser does an HTTP GET when entering a URL in the address bar and pressing enter.  The MVC framework looks for a GET Action and returns a 404 if a GET Action is not found.

    Tuesday, June 16, 2020 4:59 PM
  • User1686398519 posted

    Hi Masoud Abedi,

    If you needs to open "http://localhost:1923/my/Insert" ,you must have a action return "Insert" view.

    I make an example,you can refer to it.

    Controller

            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Index(Customer_Registration cg)
            {
                //I omitted some code to add cg to the database
                return View();
            }
            public ActionResult Insert()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Insert(Product pr)
            {
                return View();
            }

    Index.cshtml

    @model MVCTest.Models.Customer_Registration
    @{
        ViewBag.Title = "Index";
    }
    <h2>Index</h2>
    @using (Html.BeginForm("Index"))
    {
        @Html.TextBoxFor(m => m.id)
        <button type="submit">submit</button>
    }
    @Html.ActionLink("Insert", "Insert", "my")

    Insert.cshtml

    @model MVCTest.Models.Product
    @{
        ViewBag.Title = "Insert";
    }
    <h2>Insert</h2>
    @using (Html.BeginForm("Insert"))
    {
        @Html.TextBoxFor(m => m.id)
        <button type="submit">submit</button>
    }
    
    @Html.ActionLink("Index", "Index","my")

    Here is the result.

    Best Regards,

    YihuiSun

    Thursday, June 18, 2020 9:11 AM