我是MVC的新手,还在学习!我正在尝试在我的网站上创建一个非常基本的应用程序,允许用户根据他们的偏好转换货币价值。我制作了网络APi并成功将服务称为我的表格。但是,在我的控制器中,我设法将货币(名称)获取到索引视图,但是一旦在文本框中输入值以生成局部视图,就无法发回表单!我的代码中我做错了什么?!
货币控制器
namespace MVC_ATM.Controllers
{
public class CurrencyController : Controller
{
[HttpGet]
// GET: Currency
public ActionResult Index()
{
CurrenciesClient Cur = new CurrenciesClient();
var listCurrency = Cur.findAll();
SelectList list = new SelectList(listCurrency,"Id", "CurrencyName");
ViewBag.listCurrencies = list;
return View();
}
[HttpPost]
public ActionResult Index(Currencies cur)
{
if (!ModelState.IsValid)
{
string errors = string.Join("<br />", ModelState.Values
.SelectMany(x => x.Errors)
.Select(x => x.ErrorMessage));
return new ContentResult { Content = errors };
var rate = Convert.ToDecimal(cur.ConversionRate);
if (cur.CurrencyName == cur.CurrencyName)
{
ModelState.AddModelError("CurrencyCountry", "Can't make the conversion for the same value");
}
else if (cur.CurrencyName != cur.CurrencyName)
{
foreach (var currency in cur.CurrencyName)
{
ViewBag.Theresult = rate * cur.Value;
}
return PartialView("_CurrencyValue");
}
}
return View();
}
}
}
货币模型
namespace Project.Model
{
public class Currencies
{
public int Id { get; set; }
public string CurrencyName { get; set; }
public string CurrencyCountry {get; set;}
public decimal Value { get; set; }
public string ConversionRate { get; set; }
}
}
索引视图
@model Project.Model.Currencies
@{
ViewBag.Title = "Index";
}
<h2>Currency</h2>
<body>
<div class="converter">
Convert: @Html.TextBoxFor(m => m.ConversionRate, new { @size = "5" })
<div class="form-group">
@Html.Label("Convert from", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("Currency List", ViewBag.listCurrencies as SelectList, "Please Select a currency")
</div>
</div>
<div class="form-group">
@Html.Label("Convert to", new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownList("Currency List", ViewBag.listCurrencies as SelectList, "Please Select a currency")
</div>
</div>
<div>
<button type="submit" class="btn btn-primary">Convert</button>
</div>
</div>
</body>
答案 0 :(得分:2)
需要注意的事项是视图中的boolean equals(int[] a, int[]b) {
if (a.length != b.length)
return false;
if (a == b)
return true;
if (hashCode(a) != hashCode(b))
return false;
Arrays.sort(a);
Arrays.sort(b);
for(int i=0; i< a.length; i++) {
if (a[i] != b[i])
return false;
return true;
}
private int hashCode(int[] a) {
int res = 0;
for(int i=0; i<a.length; i++) {
res ^= a[i]
}
return res;
}
操作和缺少POST
标记。您创建了一个接受form
模型的POST操作,但表单没有发布。只有Currencies
将绑定到模型。要获得“货币来自”和“货币到”以及“转换率”,您将需要不同的方法/小的更改。
ConversionModel.cs一个新的索引页面模型,它将捕获您需要的字段。
ConversionRate
获取:虽然你所做的事情没有任何问题,但我们可以使用模型方法并紧密绑定它。
public class ConversionModel
{
[Required]//decimal would be better but up to you requirement
public decimal ConversionRate { get; set; }
[Required]
public int FromCurrencyId {get;set;}
public SelectList FromCurrencies {get;set;}
[Required]
public int ToCurrencyId {get;set;}
public SelectList ToCurrencies {get;set;}
}
发布:此处需要注意的重要事项是public ActionResult Index()
{
CurrenciesClient Cur = new CurrenciesClient();
var listCurrency = Cur.findAll();
ConversionModel model = new ConversionModel();
model.FromCurrencies = new SelectList(listCurrency,"Id", "CurrencyName");
model.ToCurrencies = new SelectList(listCurrency,"Id", "CurrencyName");
return View(model);
}
将不会被回发。仅发送SelectList
,ConversionRate
和FromCurrencyId
而不是列表。如果发生错误,您将需要重建列表并将其发送回模型中。
ToCurrencyId
查看:
[HttpPost]
public ActionResult Index(ConversionModel curModel)
{
if(ModelState.IsValid)
{
if(curModel.FromCurrencyId ==curModel.ToCurrencyId)
{
//do something if same currecnies and return.
}
else
{
//Get the currencyList with rates from db
//use currency ToCurrencyId and FromCurrencyId to fetch the 2 currencies
// perform conversion with curModel.ConversionRate with existing logic
}
}
//Don'f forget to rebuild the Select Lists...
return View(curModel);
}
不是CUT_COPY_PASTE。请检查是否有错误。这只是一种方法。
@model Project.Model.ConversionModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm("Index", "Currency", FormMethod.Post)
{
@Html.TextBoxFor(m => m.ConversionRate, new { @size = "5" })
@* Please check the syntax *@
@Html.DropDownListFor(m => m.FromCurrencyId , Model.FromCurrencies as SelectList)
@Html.DropDownListFor(m => m.ToCurrencyId , Model.ToCurrencies as SelectList)
<button type="submit" class="btn btn-primary">Convert</button>
}
POST可能是接下来要学习的东西......请告诉我们。
答案 1 :(得分:1)
你需要将你的物品放在这样的表格中:
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action {
Stop-Process iris -force
}