Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello everyone,

I am developing a mvc4 application. I am in trouble using models in view. Let me explain the stracture.

I have a model class that operates some methods and returns something. The name is KisiBLL.cs
I have a Controller class. That checks the actions. KisiController.cs
And I have 2 different view files.

I think as a user of this software, so I am trying to make the usage of software easier as much as possible.

In the ekle.cshtml view file, user enters the data of a person. When clicks on the add button the software should find the similar people and list to the user. The below people are found. Did you mean one of them? If user says yes, no need to add. If user says no, add button will be enabled and phisical add operation should be done.

The problem I faced, I am doing update in the same view. So when I returned a particular person's data, I cant return list of people that the user searched.

KisiBLL.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Panelium.Models
{
    public class KisiBLL
    {
        PaneliumEntities db = new PaneliumEntities();
        public void InsertorUpdate(kisi _model)
        {
            if (_model.KisiID !=0)
            {
                var secimyap = Select(_model.KisiID);
                secimyap.Ad = _model.Ad;
                secimyap.Soyadi = _model.Soyadi;
                secimyap.Turu = _model.Turu;
                secimyap.Eposta = _model.Eposta;
            }
            else
            {
                kisi _kisi = new kisi
                {
                    KisiID= SonuncuyuDonder() +1,
                    Ad = _model.Ad,
                    Soyadi = _model.Soyadi,
                    Turu = _model.Turu,
                    Eposta = _model.Eposta,
                };
                //db.AddObject("Kategori", _kategori);
                db.kisi.Add(_kisi);
            }
            db.SaveChanges();
        }
        public kisi Select(int Id)
        {
            var secimyap = db.kisi.FirstOrDefault(f => f.KisiID == Id);
            if (secimyap != null)
            {
                return secimyap;
            }
            else
            {
                return new kisi();
            }
        }
        //Returns the list person's Id in the Kisi table
        public int SonuncuyuDonder()
        {
            return db.kisi.OrderByDescending(o => o.KisiID).FirstOrDefault().KisiID;
        }
        public IEnumerable<kisi> List()
        {
            return db.kisi.ToList().OrderByDescending(o=>o.KisiID).Take(50);
        }
        //List the similar person that the user search.
        public IEnumerable<kisi> VarOlanlariListele(string _Ad , string _Soyad, string _Eposta)
        {
            var _benzerKisiler= db.kisi.Where(w => w.Ad.Contains(_Ad) || w.Soyadi.Contains(_Soyad) || w.Eposta.Contains(_Eposta)).OrderBy(o => o.Ad).ToList();
            return _benzerKisiler;
        }

    }
}


KisiController.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Panelium.Models;

namespace Panelium.Controllers
{
    public class KisiController : Controller
    {
        //
        // GET: /Kisi/
        KisiBLL _kisi = new KisiBLL();
        public ActionResult Index()
        {
            return View(_kisi.List());
        }
       
        public ActionResult Ekle(int Id = 0)
        {
            return View(_kisi.Select(Id));
        }

        [HttpPost]
        public ActionResult Ekle(kisi _model)
        {
            // There are problems here. I couldnt find a smart solution. So don't care about if true and etc.
            return View(_kisi.VarOlanlariListele(_model.Ad, _model.Soyadi, _model.Eposta));
            if(true)
            {
             _kisi.InsertorUpdate(_model);
            return RedirectToAction("Index", "Kisi");
            }
        }


    }
}


Ekle.cshtml
C#
@{
    
}@model Panelium.Models.kisi
@model IEnumarableList<Panelium.Models.kisi>// This is not possible. Problem is here. I cant return 2 models in 1 view.
<h2>Ekle</h2>
<form method="post" action=""> 
<p>
    Adı:<input type="text" name="Ad" value="@Model.Ad" />
</p>
<p>
    Soyadi:<input type="text" name="Soyadi" value="@Model.Soyadi" /></p>
<p>
    Türü:<input type="text" name="Turu" value="@Model.Turu" />
</p>

<p>
    Eposta:<input type="text" name="Eposta" value="@Model.Eposta" />
</p>

<p>
    <input type="submit" value="Ekle" />
</p>
</form>


Thanks for the responces.
Emre.
Posted
Updated 3-Oct-13 20:53pm
v2

1 solution

You should create a viewModel. ViewModel is nothing but a csharp class which contain whatever the data's displayes in one view. For example first of all create a class which contain all the properties in the kisi model class needed in the view .

C#
public class SampleViewModel
{
public SampleViewModel()
{
Kisis=new List<panelium.models.kisi>();
}

//initialize all the properties in the kisi model here

//initialize a list of kisi properties here
public List<panelium.models.kisi> Kisis{get;set;}
}
</panelium.models.kisi></panelium.models.kisi>


Then you should change the return type of view model in the view from @model Panelium.Models.kisi
@model IEnumarableList<panelium.models.kisi> to
@model SampleViewModel{}
HTML
<form method="post" action=""> 
<p>
    Adı:<input type="text" name="Ad" value="@Model.Ad" />
</p>
<p>
    Soyadi:<input type="text" name="Soyadi" value="@Model.Soyadi" /></p>
<p>
    Türü:<input type="text" name="Turu" value="@Model.Turu" />
</p>
 
<p>
    Eposta:<input type="text" name="Eposta" value="@Model.Eposta" />
</p>
 
<p>
    <input type="submit" value="Ekle" />
</p>
</form>
//if you want to iterate the List<kisi> you can use like below code
@foreach(var item in Model.Kisis)
{
//logic here
}

</kisi>


from the action result you can you should return your viewmodel class. For example
C#
 [HttpPost]
        public ActionResult Index(SampleViewModel _model)
{
var viewModel=new SampleViewModel();
viewModel.Kisis=_kisi.List();
return(viewModel);
}


C#
 [HttpPost]
        public ActionResult Ekle(SampleViewModel_model)
{
}


Hope you understand the concept of viewmodel
 
Share this answer
 
Comments
FoxRoot 4-Oct-13 6:40am    
Thanks for the awesome explanation. I was thinking about oop. I was not using properties get set. Again thank you :) I will use this manner anymore.
Jameel VM 4-Oct-13 8:57am    
welcome...use viewmodel as best practice.
FoxRoot 4-Oct-13 6:43am    
And One more question. Where should I create ViewModelClass? İn views folder or models folder?
Jameel VM 4-Oct-13 8:55am    
if your project is large it's better to keep viewmodel in a separate class library else keep it on model folder
FoxRoot 4-Oct-13 8:59am    
It will be a CRM project. Yes it is large and getting larger. I will keep in mind. Thanks. Have a nice day.

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