此时我希望文本区域为空,因为我已将ViewData ["SomeText"]设置为string.Empty
为什么在发布操作后文本区域值不会更新为空字符串?
以下是行动:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Message(int ID)
{
ViewData["ID"] = ID;
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Message(int ID, string SomeText)
{
// save Text to database
SaveToDB(ID, SomeText);
// set the value of SomeText to empty and return to view
ViewData["SomeText"] = string.Empty;
return View();
}
Run Code Online (Sandbox Code Playgroud)
和相应的观点:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm())
{ %>
<%= Html.Hidden("ID", ViewData["ID"])%>
<label for="SomeText">SomeText:</label>
<%= Html.TextArea("SomeText", ViewData["SomeText"]) …Run Code Online (Sandbox Code Playgroud) 我的Index.cshtml
@model ReportGenerator.WebUI.Models.ReportViewModel
@Html.TextBoxFor(m => m.report.FileName, new { @class = "form-control", id = "FileName" })
Run Code Online (Sandbox Code Playgroud)
我的控制器
public ActionResult Index(ReportViewModel model)
{
...some stuff
model.report = new Report();
model.report.FileName = "INDEX";
return View(model);
}
public ActionResult fillFields(ReportViewModel _model)
{
...some stuff
_model.report = new Report();
_model.report.FileName = "FILL";
return View("Index", _model);
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,该TextBox Text属性设置为"INDEX".此外,当我点击一个调用fillFields控制器动作的按钮时,TextBox仍然显示"INDEX",它不会变为"FILL".
我究竟做错了什么?为什么不想工作?
我有一个视图模型:
public class RegisterModel
{
...
public bool Confirmation{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在我的视图中使用复选框助手:
@model RegisterModel
......
@Html.CheckBoxFor(m => m.Confirmation)
Run Code Online (Sandbox Code Playgroud)
这个复选框 html 帮助器创建:
<input id="Confirmation" name="Confirmation" value="true" type="checkbox">
<input name="Confirmation" value="false" type="hidden">
Run Code Online (Sandbox Code Playgroud)
在控制器上
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (!ModelState.IsValid)
return View(model);
.....
}
Run Code Online (Sandbox Code Playgroud)
假设某些用户将输入值更改为“xxx”并发布。因此,模型无效,我们返回视图。之后,Html.CheckBoxFor给出这个错误:
从“System.String”类型到“System.Boolean”类型的参数转换失败。
内部异常:
System.FormatException:xxx 不是有效的布尔值
当我们返回 view 时:Model.Confirmationvalue 为false但Request["Confirmation"]value 为'xxx'。
此错误来自方法ValueProviderResult上的类ConvertSimpleType。我认为,它尝试将Request["Confirmation"]值转换为布尔值并且给出错误。
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Conversion failure is …Run Code Online (Sandbox Code Playgroud) 这是一个棘手的解释,所以我会尝试子弹指向.
问题:
我发现了什么:
只有在Razor代码中使用HTML Helpers时才会发生这种情况.如果我使用传统<input>元素(不理想),它工作正常.
题:
有没有人遇到过这个问题?或者有人知道为什么会这样,或者我做错了什么?
请查看下面的代码并感谢您查看我的问题!
控制器:
[HttpGet]
public ActionResult Index()
{
List<Car> cars = new List<Car>
{
new Car { ID = 1, Make = "BMW 1", Model = "325" },
new Car { ID = 2, Make = "Land Rover 2", Model = "Range Rover" },
new Car { ID = 3, Make = "Audi 3", Model = "A3" },
new Car { ID = 4, Make …Run Code Online (Sandbox Code Playgroud) 在MVC Form Post中使用隐藏字段时遇到问题.当通过HTML Helper生成隐藏字段时,它不会在回发期间保留它的值.但是当使用HTML标签时,它可以正常工作.
不幸的是,这个人花了我一整天的时间来完成这项工作.
这是我正在做的...(原谅任何拼写,重新输入的代码为SO):
查看模型
public class SomeViewModel
{
public int MyProperty1 { get; set; }
public int MyProperty2 { get; set; }
public int MyProperty3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
发布方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyActionMethod(SomeViewModel someViewModel, string command)
{
...
...
// someViewModel.MyProperty1
...
...
}
Run Code Online (Sandbox Code Playgroud)
视图
@using (Html.BeginForm("MyActionMethod", "SomeController", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.MyProperty1)
<div class="col-md-2">
<input type="hidden" value=@Model.MyProperty1 name="MyProperty1" />
<input type="submit" name="command" value="Button1" …Run Code Online (Sandbox Code Playgroud) 我有一个提交模型的表单.控制器将该模型保存到数据库,然后创建新模型并返回具有该新模型的EditCreate视图.
我遇到的问题是没有显示新模型.而是显示刚刚发布的模型.我通过调试器跟踪它到视图并看到值是新的,但是当页面显示时,它显示刚刚提交的所有旧值.
调节器
[HttpPost]
public ActionResult AddProduct(MyEntityModel myModel)
{
// Save
if (myModel.ID != 0) {
Update(myModel);
} else {
Add(myModel);
}
ModelState.Clear(); // This fixed it
// Show page for new model
MyEntityModel newModel = new MyEntityModel();
newModel.ID = 0;
// Edit/Create page
ActionResult ret = EditCreateRow(newModel);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
模型
public partial class STM_AuditData
{
public int ID { get; set; }
public Nullable<System.DateTime> ServiceDate { get; set; }
public string Product { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图 …
童话般的例行情况,我需要允许用户从 ASP.NET MVC 应用程序中的 Enum 中选择一个值。由于我使用的是 MVC 5.2 版,因此我可以简单地执行以下操作:
// Model
public MyEnum EnumVal { get; set; }
// Controller
return View(new MyModel{EnumVal = MyEnum.SomeVal});
// View
@Html.EnumDropDownListFor(model => model.EnumVal)
Run Code Online (Sandbox Code Playgroud)
这一切都神奇地起作用了。我已经做了足够多的时间来认为这是理所当然的。
但我刚刚注意到我传递的值没有被选为下拉菜单的当前值。
我可以吐出模型的内容来验证该值是否被正确传递,但下拉菜单只选择了第一个值。
其他答案(例如这里和这里)似乎证实了我认为我知道的:我不需要做任何额外的事情来让下拉菜单选择我通过的值。
我有脑放屁吗?为什么这不起作用?
更新
这是控制器方法的更完整列表:
public ActionResult Create(string enumVal)
{
var valueToPass = AccountsDestination.QbDUk;
Enum.TryParse(enumVal, out valueToPass);
return View(new QuickBooksMappingViewModel(){EnumVal = valueToPass});
}
Run Code Online (Sandbox Code Playgroud) 由于某种原因,.NET Core (2.1.4) MVC 中的 HiddenFor 帮助程序没有在隐藏输入元素上设置值,尽管它表示的模型属性上存在值。
如果我手动创建隐藏属性并使用 IdFor、NameFor 并手动设置值,它将按预期工作。
// This works and has a value attribute with a value
<input type="hidden" id="@Html.IdFor(x => x.Fields[componentIndex].Value)" name="@Html.NameFor(x => x.Fields[componentIndex].Value)" value="@Model.Fields[componentIndex].Value"/>
// This has a value attribute but it's empty
@Html.HiddenFor(x => x.Fields[componentIndex].Value)
Run Code Online (Sandbox Code Playgroud)
请注意,在这两种情况下,生成的名称和 ID 是相同的。
如果我使用 HiddenFor 并提供 anew { @Value=... }值,它也不会设置。
设置值的 HiddenFor 之间的唯一区别(使用相同的代码但不同的组件索引)是它们是在发布时提供的,而这个有问题的值是在控制器中设置的。
asp.net-mvc ×7
c# ×7
razor ×5
.net ×1
asp.net ×1
asp.net-core ×1
enums ×1
forms ×1
html ×1
html-helper ×1