locked
Views in controller RRS feed

  • Question

  • User1735445714 posted

    I created a view page which is an insertion form for a table. I want to create another insertion form for other tables in the same controller but not working, I run the program in Chrome it says "not found" how to create multiple views in one controller???

    Tuesday, June 16, 2020 3:52 PM

All replies

  • User475983607 posted

    Masoud Abedi

    I created a view page which is an insertion form for a table. I want to create another insertion form for other tables in the same controller but not working, I run the program in Chrome it says "not found" how to create multiple views in one controller???

    You're mistaken.

    Views return Actions.  A Controller typically contains many Actions.  Can you share your code so the community can see what you are trying to do?

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

    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:18 PM
  • User1735445714 posted

    i want to create for the Insert Action.

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

    i want to create for the Insert Action.

    You shared an Insert Action above.

    What is not working?   Are you trying to send an HTTP POST to the Insert Action from the Index View?   It is helpful if you explain the expected results, actual results, and enough code to reproduce the problem.

    Otherwise, I recommend going through the Getting Started Tutorials on this site.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/getting-started

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

    Hi Masoud Abedi,

    I need to confirm some questions with you:

    • What does the phrase "create another insertion form for other tables in the same controller" mean?
    • What does the phrase "how to create multiple views in one controller" mean?As far as I know, an action returns a view.

    Can you describe your problem in more detail?

    You can refer to this link to better understand MVC.

    Best Regards,

    YihuiSun

    Thursday, June 18, 2020 9:30 AM
  • User323983933 posted

    Views are in the Views folder and controllers are in the Controllers folder.

    For instance if you have a model "Customers" in your models, you can create a "CustomersController" in your controllers folder.  This Controller had Actions for all the events it handles, like "Create", "Update", "Delete", "Index"

    The views are not stored in the controller, they are used by it.

    For instance, /Views/Customers/Index.cshtml is a view.

    Controllers are able to handle multiple Actions, and therefore use many views.  

    So start with your model, usually imported from an ado.net entity. each "table" (or model) can have a Controller, and each controller can have as many actions as you need.

    Friday, June 19, 2020 3:36 AM