每当我运行此代码时,我都有问题从控制器传递结果以从httppost方法查看。服务器端代码很好,只是当我在calculator.Result属性中分配一个新结果时,我从未在视图的文本框中看到新分配的值。其余的属性都填充了适当的值。
有谁可以请帮助我如何解决这个问题?或者我错过了什么?
谢谢
这是我的代码来自.cshtml
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.FirstNumber)
</div>
<div class="span2">
@Html.TextBoxFor(x => x.FirstNumber)
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.FirstNumber)
</div>
</div>
<br />
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.SecondNumber)
</div>
<div class="span2">
@Html.TextBoxFor(x => x.SecondNumber)
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.SecondNumber)
</div>
</div>
<br />
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.SelectedFunction)
</div>
<div class="span2">
@Html.DropDownListFor(x => x.SelectedFunction, Model.Functions, "Please Make a Selection")
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.SelectedFunction)
</div>
</div>
<br/>
<div class="row">
<div class="span2">
@Html.Label("Result: ")
</div>
<div class="span2">
@Html.TextBoxFor(x => x.Result)
</div>
</div>
<br />
<div class="row">
<input type="submit" value="Load" />
</div>
}
这是我在控制器后面的代码
[HttpPost]
public ActionResult Index(CalculatorViewModels calculator)
{
//If the entries are valid
if (ModelState.IsValid)
{
try
{
double firstNum = calculator.FirstNumber;
double secondNum = calculator.SecondNumber;
string selectedFunction = calculator.SelectedFunction;
double result = -1;
if (selectedFunction.Equals("1"))
{
result = service.CombinedWith(firstNum, secondNum);
}
else if (selectedFunction.Equals("2"))
{
result = service.Either(firstNum, secondNum);
}
calculator.Result = Convert.ToString(result);
return View(calculator);
}
catch(Exception ex)
{
calculator.Result = "Error in calculating probability "+ex.Message;
return View(calculator);
}
}
//entries are not valid
else
{
return View(calculator);
}
}
这是我的视图模型
公共类CalculatorViewModels {
[Required(ErrorMessage = "Please enter a Number between zero and one")]
[Display(Name = "FirstNumber")]
[Range(0, 1)]
public double FirstNumber { get; set; }
[Required(ErrorMessage = "Please enter a Number between zero and one")]
[Display(Name = "Second Number")]
[Range(0, 1)]
public double SecondNumber { get; set; }
[Required(ErrorMessage = "Please Select at least one function")]
[Display(Name="Please Select a Function")]
public string SelectedFunction { get; set; }
//will be used to report exception as well if needed
public string Result { get; set; }
public List<SelectListItem> Functions
{
get
{
return new List<SelectListItem>()
{
new SelectListItem{ Text="CombinedWith",Value="0"},
new SelectListItem{Text="Either",Value="1"}
};
}
}
}
答案 0 :(得分:0)
在视图的顶部,转储模型的内容仅用于进行健全性检查。使用HTML.Raw(..)或其他东西,然后查看页面源以检查结果是否实际设置。
像
这样的东西@{
var model = @Html.Raw(Json.Encode(Model));
}
使用JavaScript将模型写出到控制台或其他东西。
让我知道你是如何进行的,以及结果是否已经确定。
答案 1 :(得分:0)
尝试这种方法......
1)更新视图,为提交按钮添加点击处理程序,并在javascript中处理
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.FirstNumber)
</div>
<div class="span2">
@Html.TextBoxFor(x => x.FirstNumber)
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.FirstNumber)
</div>
</div>
<br />
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.SecondNumber)
</div>
<div class="span2">
@Html.TextBoxFor(x => x.SecondNumber)
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.SecondNumber)
</div>
</div>
<br />
<div class="row">
<div class="span2">
@Html.LabelFor(x => x.SelectedFunction)
</div>
<div class="span2">
@Html.DropDownListFor(x => x.SelectedFunction, Model.Functions, "Please Make a Selection")
</div>
<div class="field-validation-error">
@Html.ValidationMessageFor(x => x.SelectedFunction)
</div>
</div>
<br/>
<div class="row">
<div class="span2">
@Html.Label("Result: ")
</div>
<div class="span2">
@Html.TextBoxFor(x => x.Result)
</div>
</div>
<br />
<div class="row">
<input type="button" value="Load" id="btnSubmit" onclick="submitForm();"/>
</div>
}
<script src="~/Scripts/jquery-2.1.3.js"></script>
<script type="text/javascript">
var url = '@Url.Action("Index")';
function submitForm() {
var data = {};
data.FirstNumber = $('#FirstNumber').val();
data.SecondNumber = $('#SecondNumber').val();
data.SelectedFunction = $('#SelectedFunction').val();
$.post(url, data).done(function (result) {
$('#Result').val(result);
});
}
</script>
修改控制器以返回Json数据
using Newtonsoft.Json;
..
..
[HttpPost]
public ActionResult Index(CalculatorViewModels calculator)
{
//If the entries are valid
if (ModelState.IsValid)
{
try
{
double firstNum = calculator.FirstNumber;
double secondNum = calculator.SecondNumber;
string selectedFunction = calculator.SelectedFunction;
double result = -1;
if (selectedFunction.Equals("1"))
{
result = service.CombinedWith(firstNum, secondNum);
}
else if (selectedFunction.Equals("2"))
{
result = service.Either(firstNum, secondNum);
}
calculator.Result = Convert.ToString(result);
return Json(result);
}
catch (Exception ex)
{
calculator.Result = "Error in calculating probability " + ex.Message;
return Json(0);
}
}
//entries are not valid
else
{
return Json(0);
}
}
不要忘记将jQuery库添加到scripts文件夹并添加对Newtonsoft.Json的引用。希望这会有所帮助。