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
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.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();
}
}
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);
}
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
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
{
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)
{
return View(_kisi.VarOlanlariListele(_model.Ad, _model.Soyadi, _model.Eposta));
if(true)
{
_kisi.InsertorUpdate(_model);
return RedirectToAction("Index", "Kisi");
}
}
}
}
Ekle.cshtml
@{
}@model Panelium.Models.kisi
@model IEnumarableList<Panelium.Models.kisi>
<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.