Asp.net mvc 将带有参数的视图模型传递到POST方法
我想用DI(纯3层应用程序)构建我的应用程序 一个ViewModel类填充Constructor中的一些字段:Asp.net mvc 将带有参数的视图模型传递到POST方法,asp.net-mvc,constructor,dependency-injection,asp.net-mvc-5,Asp.net Mvc,Constructor,Dependency Injection,Asp.net Mvc 5,我想用DI(纯3层应用程序)构建我的应用程序 一个ViewModel类填充Constructor中的一些字段: public class RegisterAsSmartphonePhotographerViewModel { public RegisterAsSmartphonePhotographerViewModel(PixlocateBusinessLogic.IStaticListRepository repository) { Country = new
public class RegisterAsSmartphonePhotographerViewModel
{
public RegisterAsSmartphonePhotographerViewModel(PixlocateBusinessLogic.IStaticListRepository repository)
{
Country = new ListViewModel<Pair<string, int>>();
State = new ListViewModel<Pair<string, int>>();
PixlocateBusinessLogic.StaticListService service = new PixlocateBusinessLogic.StaticListService(repository);
var countries = service.CountryList();
var states = service.StateList();
foreach (var item in countries)
{
Country.List.Add(new System.Web.Mvc.SelectListItem() { Text = item.CountryName, Value = item.CountryID.ToString() });
}
foreach (var item in states)
{
State.List.Add(new System.Web.Mvc.SelectListItem() { Text = item.StateName, Value = item.StateID.ToString() });
}
}
[Required]
public string Username { get; set; }
[Required(ErrorMessage ="Country is required")]
public int CountryID { get; set; }
public ListViewModel<Pair<string, int>> Country { get; set; }
[Required(ErrorMessage = "State is required")]
public int StateID { get; set; }
public ListViewModel<Pair<string, int>> State { get; set; }
}
公共类注册表SmartPhonePhotographiewModel
{
公共注册表SmartPhonePicturerViewModel(PixlocateBusinessLogic.IStaticListRepository存储库)
{
国家/地区=新的ListViewModel();
State=新的ListViewModel();
PixlocateBusinessLogic.StaticListService服务=新的PixlocateBusinessLogic.StaticListService(存储库);
var countries=service.CountryList();
var states=service.StateList();
foreach(国家/地区的var项目)
{
Country.List.Add(new System.Web.Mvc.SelectListItem(){Text=item.CountryName,Value=item.CountryID.ToString()});
}
foreach(状态中的var项目)
{
State.List.Add(new System.Web.Mvc.SelectListItem(){Text=item.StateName,Value=item.StateID.ToString()});
}
}
[必需]
公共字符串用户名{get;set;}
[必需(ErrorMessage=“国家/地区是必需的”)]
public int CountryID{get;set;}
public ListViewModel国家{get;set;}
[必需(ErrorMessage=“State is Required”)]
public int StateID{get;set;}
公共ListViewModel状态{get;set;}
}
和我的控制器:
[AllowAnonymous]
public ActionResult RegisterAsSmartphonePhotographer()
{
ViewModels.RegisterAsSmartphonePhotographerViewModel model = new ViewModels.RegisterAsSmartphonePhotographerViewModel(_staticListRepository);
return View(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> RegisterAsSmartphonePhotographer(ViewModels.RegisterAsSmartphonePhotographerViewModel model)
{
if (ModelState.IsValid)
{
// some actions
}
return View(model);
}
[AllowAnonymous]
公共行动结果登记处SmartPhoneScope()
{
ViewModels.registerasmartphoneaphotorviewmodel model=新的ViewModels.registerasmartphoneaphotorviewmodel(\u staticListRepository);
返回视图(模型);
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务注册表sSmartPhoneScopector(ViewModels.RegistersSmartPhoneScoperViewModel)
{
if(ModelState.IsValid)
{
//一些行动
}
返回视图(模型);
}
当然,我有一个错误:
没有为此对象定义无参数构造函数
当我将表单发布到服务器时。
当然,我不能从RegisterAsmartPhonePhotographIEWModel构造函数中删除参数。
我怎样才能说“传递参数”给POST方法?问题是,当您发布表单数据时,DefaultModelBinder将尝试创建视图模型的对象,并尝试将发布的表单数据映射到视图模型对象的属性。由于视图模型没有无参数构造函数,因此在尝试创建视图模型的对象时失败
public class RegisterAsSmartphonePhotographerViewModel
{
public RegisterAsSmartphonePhotographerViewModel(){}
public RegisterAsSmartphonePhotographerViewModel(IStaticListRepository repository)
{
// Existing code
}
// Existing code
}
您需要在视图模型中创建默认的无参数构造函数
public class RegisterAsSmartphonePhotographerViewModel
{
public RegisterAsSmartphonePhotographerViewModel(){}
public RegisterAsSmartphonePhotographerViewModel(IStaticListRepository repository)
{
// Existing code
}
// Existing code
}
同样,在视图模型类中混合数据访问代码不是一个好主意。视图模型应该是精简的POCO类,专门用于在视图和操作方法之间传输数据。如果您试图将数据访问代码混入其中,则会造成紧密耦合并打破单一责任原则 您应该更改视图模型中的数据/webservice调用,并将其移动到另一层(操作方法/BL等) 简单样本
public class CustomerController : Controller
{
IStaticListRepository repository;
public CustomerController(IStaticListRepository repository)
{
this.repository= repository;
}
public ActionResult Create()
{
var vm = new RegisterAsSmartphonePhotographerViewModel();
// using the repository object, populate the vm properties
return View(vm);
}
}
问题是,发布表单数据时,DefaultModelBinder将尝试创建视图模型的对象,并尝试将发布的表单数据映射到视图模型对象的属性。由于视图模型没有无参数构造函数,因此在尝试创建视图模型的对象时失败
public class RegisterAsSmartphonePhotographerViewModel
{
public RegisterAsSmartphonePhotographerViewModel(){}
public RegisterAsSmartphonePhotographerViewModel(IStaticListRepository repository)
{
// Existing code
}
// Existing code
}
您需要在视图模型中创建默认的无参数构造函数
public class RegisterAsSmartphonePhotographerViewModel
{
public RegisterAsSmartphonePhotographerViewModel(){}
public RegisterAsSmartphonePhotographerViewModel(IStaticListRepository repository)
{
// Existing code
}
// Existing code
}
同样,在视图模型类中混合数据访问代码不是一个好主意。视图模型应该是精简的POCO类,专门用于在视图和操作方法之间传输数据。如果您试图将数据访问代码混入其中,则会造成紧密耦合并打破单一责任原则 您应该更改视图模型中的数据/webservice调用,并将其移动到另一层(操作方法/BL等) 简单样本
public class CustomerController : Controller
{
IStaticListRepository repository;
public CustomerController(IStaticListRepository repository)
{
this.repository= repository;
}
public ActionResult Create()
{
var vm = new RegisterAsSmartphonePhotographerViewModel();
// using the repository object, populate the vm properties
return View(vm);
}
}
“另外,在视图模型类中混合数据访问代码也不是一个好主意。”-我在哪里错过了它?我从作为抽象(接口)传递的特殊类调用它。在您的示例中,您必须两次填充模型(在GET和PUT方法中),另外,我想使用模型验证,如何在没有传递存储库的情况下调用它?“视图模型应该是精简的平面POCO类”-例如,最好使用视图模型,它可以验证自身(即实现IValidatable)。它根本不是POCO类“在视图模型类中混合数据访问代码也不是一个好主意。”-我在哪里错过了它?我从作为抽象(接口)传递的特殊类调用它。在您的示例中,您必须两次填充模型(在GET和PUT方法中),另外,我想使用模型验证,如何在没有传递存储库的情况下调用它?“视图模型应该是精简的平面POCO类”-例如,最好使用视图模型,它可以验证自身(即实现IValidatable)。这根本不是POCO课程