Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
CSS
This is model class file:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVC.Models
{
    public class Idetails
    {
        private int Id;
        private string Name;
        private string City;


        public int id
        {
            set
            {
                Id = value;
            }
            get
            {
                return Id;
            }
        }

        public string name
        {
            set
            {
                Name = value;
            }
            get
            {
                return Name;
            }
        }

        public string city
        {
            set
            {
                City = value;
            }
            get
            {
                return City;
            }
        }
    }
}



This is the controller file:-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using System.Data.SqlClient;
using System.Data;

namespace MVC.Controllers
{
    public class DetailsController : Controller
    {
        //
        // GET: /Details/

        public ActionResult det()
        {
            Idetails i = new Idetails();
            SqlConnection con = new SqlConnection("Data Source=PRABAKARAN\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into ct values("+i.id+",'"+i.name+"','"+i.city+"')", con);
            cmd.ExecuteNonQuery();
            return View(i);
        }

    }
}

In that views i dont know how to write the query in design page because the code page is not open..

please help me to solve this..
Posted

public ActionResult det()
{
Connection();
Idetails i = new Idetails();
con_Open();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand("MVC_Insert", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", 1);
cmd.Parameters.AddWithValue("@Name", "Himanshu");
cmd.Parameters.AddWithValue("@City", "Lucknow");
int ins = cmd.ExecuteNonQuery();
con_Close();
return View(i);
}

public void Connection()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
}

public static void con_Open()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
}
catch (Exception ex)
{

}
}

public void con_Close()
{
try
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Close();
}
catch (Exception ex)
{
}
}
 
Share this answer
 
Your view should point to the model you have created. and in your controller you have access to your model. This is how they are linked. In you view using Model dot you can access all the methods.
Refer this for more help
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900