我有一个非常基本的代码来实现基本的http帖子。当我点击按钮时,我希望执行流程为"公共ActionResult DepositToAcount(DepositToAccountView模型)"但它没有。我错过了什么?这是代码:提前谢谢。
public class MainController : Controller
{
public ActionResult DepositToAcount()
{
FindAllBankAccountResponse response = null;
DepositToAccountView viewmodel = null;
try
{
response = new ApplicationBankAccountService().GetAllBankAccounts();
viewmodel = new DepositToAccountView();
viewmodel.SetupList(response);
}
catch (Exception ex)
{
throw ex;
}
return View(viewmodel);
}
[HttpPost]
public ActionResult DepositToAcount(DepositToAccountView model)
{
//code execution never reachs there.
DepositToAccountView gorunum = null;
try
{
gorunum = new DepositToAccountView();
//deposit implementation
}
catch (Exception ex)
{
throw ex;
}
return View(gorunum);
}
}
以下是观点:
@model AppService.ViewModel.DepositToAccountView
@{
ViewBag.Title = "Deposit To Acount Page";
}
<h2>@ViewBag.Title</h2>
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.ChosenAccount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.ChosenAccount, Model.AccountList)
@Html.ValidationMessageFor(model => model.ChosenAccount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AmountToBeDeposited, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AmountToBeDeposited, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AmountToBeDeposited, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Deposit" class="btn btn-default" />
</div>
</div>
</div>
这是DepositToAccountView:
public class DepositToAccountView
{
private string _amountToBeDeposited;
private string _chosenAccount;
private List<SelectListItem> _accountList;
public List<SelectListItem> AccountList
{
get
{
return _accountList;
}
set
{
_accountList = value;
}
}
[Display(Name = "Para Eklemek İstediğiniz Hesabı Seçiniz:")]
public string ChosenAccount
{
get
{
return this._chosenAccount;
}
set
{
this._chosenAccount = value;
}
}
[Display(Name = "Eklenecek miktarı giriniz: ")]
[DataType(DataType.Currency)]
[Required(ErrorMessage = "Lütfen bir miktar giriniz.")]
public string AmountToBeDeposited
{
get
{
return this._amountToBeDeposited;
}
set
{
this._amountToBeDeposited = value;
}
}
public DepositToAccountView()
{
this.AccountList = new List<SelectListItem>();
}
public void SetupList(FindAllBankAccountResponse Response)
{
SelectListItem item = null;
try
{
if (Response == null)
{
return;
}
this.allDefaultValueToList();
foreach (BankAccountView gorunum in Response.BankAccountView)
{
item = new SelectListItem();
item.Text = gorunum.CustomerRef;
item.Value = gorunum.AccountNo.ToString();
this.AccountList.Add(item);
}
}
catch (Exception)
{
throw;
}
}
private void allDefaultValueToList()
{
SelectListItem secinizHesap = new SelectListItem();
secinizHesap.Value ="0";
secinizHesap.Text = "Hesap Seçiniz";
this.AccountList.Add(secinizHesap);
}
}
答案 0 :(得分:0)
似乎你没有<form>
。它应该提交什么? :)
所以如果你改变你的观点:
@model AppService.ViewModel.DepositToAccountView
@{
ViewBag.Title = "Deposit To Acount Page";
}
<h2>@ViewBag.Title</h2>
@using(Html.BeginForm())
{
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.ChosenAccount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.ChosenAccount, Model.AccountList)
@Html.ValidationMessageFor(model => model.ChosenAccount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AmountToBeDeposited, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AmountToBeDeposited, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AmountToBeDeposited, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Deposit" class="btn btn-default" />
</div>
</div>
</div>
}
它应该按预期工作。 Html.BeginForm()
这里添加了起始表单元素,当它放在using
语句的末尾时,它会写入结束元素。