使用 Viewbag 将列表发送到视图是错误的。在这种情况下,您应该使用 Viewmodel。像这样:
发送您需要在视图中显示的国家/地区列表和城市列表以及其他列表:
家庭控制器代码:
[HttpGet]
public ActionResult NewAgahi() // New Advertising
{
//--------------------------------------------------------
// استفاده از ویومدل برای نمایش چند مدل در ویو
Country_Repository blCountry = new Country_Repository();
Ostan_Repository blOstan = new Ostan_Repository();
City_Repository blCity = new City_Repository();
Mahale_Repository blMahale = new Mahale_Repository();
Agahi_Repository blAgahi = new Agahi_Repository();
var vm = new NewAgahi_ViewModel();
vm.Country = blCountry.Select();
vm.Ostan = blOstan.Select();
vm.City = blCity.Select();
vm.Mahale = blMahale.Select();
//vm.Agahi = blAgahi.Select();
return View(vm);
}
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult NewAgahi(Agahi agahi)
{
if (ModelState.IsValid == true)
{
Agahi_Repository blAgahi = new Agahi_Repository();
agahi.Date = DateTime.Now.Date;
agahi.UserId = 1048;
agahi.GroupId = 1;
if (blAgahi.Add(agahi) == true)
{
//Success
return JavaScript("alert('ثبت شد')");
}
else
{
//Fail
return JavaScript("alert('اشکال در ثبت')");
}
视图模型代码:
using ProjectName.Models.DomainModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ProjectName.ViewModels
{
public class NewAgahi_ViewModel // برای استفاده کردن از چند مدل در یک ویو
{
public IEnumerable<Country> Country { get; set; }
public IEnumerable<Ostan> Ostan { get; set; }
public IEnumerable<City> City { get; set; }
public IQueryable<Mahale> Mahale { get; set; }
public ProjectName.Models.DomainModels.Agahi Agahi { get; set; }
}
}
查看代码:
@model ProjectName.ViewModels.NewAgahi_ViewModel
…………
@Html.DropDownList("CountryList", new SelectList(Model.Country, "id", "Name"))
@Html.DropDownList("CityList", new SelectList(Model.City, "id", "Name"))
Country_Repository 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ProjectName.Models.DomainModels;
namespace ProjectName.Models.Repositories
{
public class Country_Repository : IDisposable
{
private MyWebSiteDBEntities db = null;
public Country_Repository()
{
db = new DomainModels.MyWebSiteDBEntities();
}
public Boolean Add(Country entity, bool autoSave = true)
{
try
{
db.Country.Add(entity);
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
//return "True";
else
return false;
}
catch (Exception e)
{
string ss = e.Message;
//return e.Message;
return false;
}
}
public bool Update(Country entity, bool autoSave = true)
{
try
{
db.Country.Attach(entity);
db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch (Exception e)
{
string ss = e.Message; // کد بلااستفاده فقط برای ازمایش اکسپشن این را نوشتم
return false;
}
}
public bool Delete(Country entity, bool autoSave = true)
{
try
{
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}
public bool Delete(int id, bool autoSave = true)
{
try
{
var entity = db.Country.Find(id);
db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
if (autoSave)
return Convert.ToBoolean(db.SaveChanges());
else
return false;
}
catch
{
return false;
}
}
public Country Find(int id)
{
try
{
return db.Country.Find(id);
}
catch
{
return null;
}
}
public IQueryable<Country> Where(System.Linq.Expressions.Expression<Func<Country, bool>> predicate)
{
try
{
return db.Country.Where(predicate);
}
catch
{
return null;
}
}
public IQueryable<Country> Select()
{
try
{
return db.Country.AsQueryable();
}
catch
{
return null;
}
}
public IQueryable<TResult> Select<TResult>(System.Linq.Expressions.Expression<Func<Country, TResult>> selector)
{
try
{
return db.Country.Select(selector);
}
catch
{
return null;
}
}
public int GetLastIdentity()
{
try
{
if (db.Country.Any())
return db.Country.OrderByDescending(p => p.id).First().id;
else
return 0;
}
catch
{
return -1;
}
}
public int Save()
{
try
{
return db.SaveChanges();
}
catch
{
return -1;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this.db != null)
{
this.db.Dispose();
this.db = null;
}
}
}
~Country_Repository()
{
Dispose(false);
}
}
}