Asp.net 通过ViewModel从多个DropDownList向控制器传递值时出错
我正在尝试创建一个表单DropDownList,该表单由SelectList创建,根据学生用户关联的不同[银行]帐户生成。到目前为止,我已经能够成功地为所有帐户创建两个下拉列表,但是当我提交表单时,我得到一个错误:没有为此对象定义无参数构造函数。 我不确定这个错误的原因是什么,我想我正确地设置了控制器和视图来处理不同的值,但是我不确定传入的ViewModel是否正确 账户模型(转账操作结果中使用的银行账户) TransferFundsViewModel(这是构建了我的强类型视图的类,我正在尝试将其传递给我的控制器。) 在我的控制器内转移资金方法Asp.net 通过ViewModel从多个DropDownList向控制器传递值时出错,asp.net,asp.net-mvc,asp.net-mvc-4,viewmodel,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Viewmodel,我正在尝试创建一个表单DropDownList,该表单由SelectList创建,根据学生用户关联的不同[银行]帐户生成。到目前为止,我已经能够成功地为所有帐户创建两个下拉列表,但是当我提交表单时,我得到一个错误:没有为此对象定义无参数构造函数。 我不确定这个错误的原因是什么,我想我正确地设置了控制器和视图来处理不同的值,但是我不确定传入的ViewModel是否正确 账户模型(转账操作结果中使用的银行账户) TransferFundsViewModel(这是构建了我的强类型视图的类,我正在尝试将
[HttpGet]
public ActionResult TransferFunds()
{
//Get logged in user
var studentProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
//Get student account associated with logged in user
var student = db.Students.Find(studentProfile.UserId);
//Get accounts associated with student account
var accounts = db.Accounts.Where(x => x.StudentID == student.UserId);
var selectionList = new SelectList(accounts, "AccountID", "AccountName");
var vm = new TransferFundsViewModel { ListOfSourceAccounts = selectionList, ListOfDestinationAccounts = selectionList };
return View(vm);
}
[HttpPost]
public ActionResult TransferFunds(TransferFundsViewModel viewModel)
{
if (ModelState.IsValid)
{
try
{
AccountManager manager = new AccountManager();
Account srcAccount = db.Accounts.Find(viewModel.SelectedSourceAccountId);
Account destAccount = db.Accounts.Find(viewModel.SelectedDestinationAccountId);
manager.TransferMoney(srcAccount, destAccount, viewModel.TransferAmount); //From Account Manager
db.SaveChanges();
}
catch (DataException)
{
ModelState.AddModelError("", "Something went wrong, transfer could not be completed.");
}
}
return RedirectToAction("AccountSummary");
}
AccountManager(该类包含实际转移资金的逻辑。)
最后,转移资金视图
@model eBank.ViewModels.TransferFundsViewModel
@using (Html.BeginForm()) {
<fieldset>
<legend>Transfer Funds</legend>
<div class="editor-label">
Source Account
</div>
<div class="editor-field">
@Html.DropDownListFor(viewModel => viewModel.ListOfSourceAccounts, Model.ListOfSourceAccounts, "Choose an Account")
</div>
<div class="editor-label">
Destination Account
</div>
<div class="editor-field">
@Html.DropDownListFor(viewModel => viewModel.ListOfDestinationAccounts, Model.ListOfDestinationAccounts, "Choose an Account")
</div>
<div class="editor-label">
Transfer Amount
</div>
<div class="editor-field">
@Html.EditorFor(viewModel => viewModel.TransferAmount)
</div>
<p>
<input type="submit" value="Transfer" />
</p>
</fieldset>
}
@model eBank.ViewModels.TransferFundsViewModel
@使用(Html.BeginForm()){
转移资金
来源帐户
@Html.DropDownListFor(viewModel=>viewModel.ListOfSourceAccounts,Model.ListOfSourceAccounts,“选择帐户”)
目的地帐户
@Html.DropDownListFor(viewModel=>viewModel.ListOfDestinationAccounts,Model.ListOfDestinationAccounts,“选择帐户”)
转账金额
@EditorFor(viewModel=>viewModel.TransferAmount)
}
创建dropdownlist时,需要更改第一个参数以使用SelectedSourceAccountId
改变
@Html.DropDownListFor(viewModel => viewModel.ListOfSourceAccounts, Model.ListOfSourceAccounts, "Choose an Account")
为了
与其他下拉列表相同
现在,您的模型应该有了值,srccount和destcount对象将不会为空。Wow,这真是太快了!非常感谢。我不敢相信修复这么简单。。。我花了至少一个半小时来试验不同的解决方案。
public class AccountManager
{
public void TransferMoney(Account srcAccount, Account destAccount, decimal transferAmount)
{
if (srcAccount.AccountTotal >= transferAmount)
{
srcAccount.AccountTotal -= transferAmount;
destAccount.AccountTotal += transferAmount;
}
}
}
@model eBank.ViewModels.TransferFundsViewModel
@using (Html.BeginForm()) {
<fieldset>
<legend>Transfer Funds</legend>
<div class="editor-label">
Source Account
</div>
<div class="editor-field">
@Html.DropDownListFor(viewModel => viewModel.ListOfSourceAccounts, Model.ListOfSourceAccounts, "Choose an Account")
</div>
<div class="editor-label">
Destination Account
</div>
<div class="editor-field">
@Html.DropDownListFor(viewModel => viewModel.ListOfDestinationAccounts, Model.ListOfDestinationAccounts, "Choose an Account")
</div>
<div class="editor-label">
Transfer Amount
</div>
<div class="editor-field">
@Html.EditorFor(viewModel => viewModel.TransferAmount)
</div>
<p>
<input type="submit" value="Transfer" />
</p>
</fieldset>
}
@Html.DropDownListFor(viewModel => viewModel.ListOfSourceAccounts, Model.ListOfSourceAccounts, "Choose an Account")
@Html.DropDownListFor(viewModel => viewModel.SelectedSourceAccountId, Model.ListOfSourceAccounts, "Choose an Account")