0

I have an application in Asp.NET MVC with Entity Data Model. Now I have a model for state_master table where I have only one property 'State_Name' and in database table I have 2 fields 'State_ID' which is auto incremented and 'State_Name' for which I will insert the data. Now as I have only one property in Model I am not able to fetch both the fields 'State_ID' and 'State_Name' using this model. This is the issue which I am facing.

State Model File :

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

namespace ViewData.Models
{
 public class state_model
      {
        [Key]
        public string state_name { get; set; }
       }
 }

View File :

@using ViewData.Models
@{
   Layout = null;
 }

  <!DOCTYPE html>
  <html>
  <head>
  <meta name="viewport" content="width=device-width" />
  <title>state_view</title>
  </head> 
  <body>
   <div> 
   <ul>
      @foreach (var state in ViewData["states"] as   IList<state_model>)
          {
            <li>
               @state.state_name
            </li>
           }
   </ul>
   </div>
   </body>
   </html>

Controller File :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ViewData.DataAccessLayer;
using ViewData.Models;
namespace ViewData.Controllers
{
 public class StateController : Controller
    {
     // GET: State
     public ActionResult Index()
        {
         DataLayer dl = new DataLayer();
         IList<state_model> data = (from o in dl.states select o).ToList();
         ViewData["states"] = data;
         return View("state_view");
         }
     }
 }
Basanta Matia
  • 1,504
  • 3
  • 15
  • 27
Zala Krunal
  • 315
  • 1
  • 3
  • 17
  • why don't you keep state_Id in your Model ? Without that how can you bind the data ?? – Basanta Matia Mar 31 '17 at 14:22
  • as my state_id is auto incremented at database field, when I insert data it throws error. – Zala Krunal Mar 31 '17 at 14:24
  • 1
    Are you using Entity Framework? EF has provision to map ID column with value generation handled by the database. You don't need to set the Id when you insert new entity. You only need to set values of other properties except Id property. – Chetan Mar 31 '17 at 14:26
  • how to do it? I am very new with Asp.NET MVC and EF. Please help – Zala Krunal Mar 31 '17 at 14:43

1 Answers1

0

You need to create a View Model for state to fetch and bind all the fields from database. For example,

public class state_ViewModel
  {
    [Key]
    public int state_Id { get; set; } //check with your data type
    public string state_name { get; set; }
   }

So in your controller method, You can use this one for fetch and bind. And for insert record you can use state_model

And in your action method,

public ActionResult Index()
    {
     DataLayer dl = new DataLayer();
     IList<state_ViewModel> data = (from o in dl.states select o).ToList();
     ViewData["states"] = data;
     return View("state_view");
     }

See, View Model is nothing but rendering your data in your view. To know more about view model click here

Community
  • 1
  • 1
Basanta Matia
  • 1,504
  • 3
  • 15
  • 27