C# Html.EditorFor设置默认值
菜鸟问题。 我有一个参数被传递给create视图。我需要用默认值设置一个字段名。 @EditorFor(model=>model.Id) 我需要将这个输入字段的名称Id设置为默认值,该值通过actionlink传递给视图 那么,这个输入字段--@Html.EditorFor(model=>model.Id)如何设置默认值呢C# Html.EditorFor设置默认值,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,菜鸟问题。 我有一个参数被传递给create视图。我需要用默认值设置一个字段名。 @EditorFor(model=>model.Id) 我需要将这个输入字段的名称Id设置为默认值,该值通过actionlink传递给视图 那么,这个输入字段--@Html.EditorFor(model=>model.Id)如何设置默认值呢 @Html.EditorFor(c => c.PropertyName, new { text = "5"; }) 以下操作是否有效?其中数字5是一个参数,我将其传递到文本字
@Html.EditorFor(c => c.PropertyName, new { text = "5"; })
以下操作是否有效?其中数字5是一个参数,我将其传递到文本字段以设置默认值
@Html.EditorFor(c => c.PropertyName, new { text = "5"; })
以下是我的发现:
@Html.TextBoxFor(c => c.Propertyname, new { @Value = "5" })
使用大写字母V,而不是小写字母V(假定值是setter中通常使用的关键字)
不起作用
但是,您的代码最终看起来是这样的
<input Value="5" id="Propertyname" name="Propertyname" type="text" value="" />
价值对价值。不知道我会不会太喜欢这个
如果proprety是否有值,如果它没有,为什么不直接检查控制器操作?在视图模型中将它设置为默认值,并让它绑定,以避免视图中的所有这些冒牌操作?更好的选择是在视图模型中这样做,如
public class MyVM
{
int _propertyValue = 5;//set Default Value here
public int PropertyName{
get
{
return _propertyValue;
}
set
{
_propertyValue = value;
}
}
}
那么在你看来,
@Html.EditorFor(c => c.PropertyName)
将按您希望的方式工作(如果没有值,则会有默认值)在视图中设置默认值是不对的。视图应该执行显示工作,而不是更多。这一行动打破了MVC模式的意识形态。因此,设置默认值的正确位置是控制器类的create方法 最简单的方法是通过控制器传递所创建实体的新实例:
//GET
public ActionResult CreateNewMyEntity(string default_value)
{
MyEntity newMyEntity = new MyEntity();
newMyEntity._propertyValue = default_value;
return View(newMyEntity);
}
ViewBag.ProductId = 1;
如果要通过ActionLink传递默认值
@Html.ActionLink("Create New", "CreateNewMyEntity", new { default_value = "5" })
我刚刚做了这个(Shadi的第一个答案),它起到了一种效果:
public ActionResult Create()
{
Article article = new Article();
article.Active = true;
article.DatePublished = DateTime.Now;
ViewData.Model = article;
return View();
}
我可以像propper MVC上瘾者一样将默认值放入模型中:(我使用的是实体框架)
有人能看出这有什么缺点吗?难道
@Html.EditorFor()
不应该使用您在模型中添加的属性吗
[DefaultValue(false)]
public bool TestAccount { get; set; }
在模型类的构造函数方法中,根据需要设置默认值。 然后在第一个操作中创建模型的实例并将其传递给视图
public ActionResult VolunteersAdd()
{
VolunteerModel model = new VolunteerModel(); //to set the default values
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult VolunteersAdd(VolunteerModel model)
{
return View(model);
}
将其推入
视图袋中
:
控制器:
//GET
public ActionResult CreateNewMyEntity(string default_value)
{
MyEntity newMyEntity = new MyEntity();
newMyEntity._propertyValue = default_value;
return View(newMyEntity);
}
ViewBag.ProductId = 1;
视图:
这是我的工作代码:
@Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control", @Value = "123" } })
与其他答案不同的是,我在htmlAttributes数组中使用了值,这对我来说很有效
处于受控状态
ViewBag.AAA = default_Value ;
在视图中
@Html.EditorFor(model => model.AAA, new { htmlAttributes = new { @Value = ViewBag.AAA } }
这对我很有效: 在控制器中
*ViewBag.DefaultValue= "Default Value";*
在视图中
*@Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter a Value", @Value = ViewBag.DefaultValue} })*
对于我,我需要将当前日期和时间设置为默认值。这解决了我在视图中的问题。添加以下代码:
<div class="form-group">
@Html.LabelFor(model => model.order_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.order_date, new { htmlAttributes = new { @class = "form-control",@Value= DateTime.Now } })
@Html.ValidationMessageFor(model => model.order_date, "", new { @class = "text-danger" })
</div>
</div>
@LabelFor(model=>model.order_date,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.order_date,new{htmlAttributes=new{@class=“form control”,@Value=DateTime.Now})
@Html.ValidationMessageFor(model=>model.order_date,“,new{@class=“text danger”})
这些是固定的默认值,如果您想在运行时提供它,您需要不同的方法。感谢@Shadi,从技术上讲是Datetime。现在不是固定值。如果默认值是在运行时生成的,那么它是模型责任还是控制器?只需参考MVC规则(您可以打破它!)模型只是用于保存数据,视图用于查看数据,您的业务应该放在控制器或控制器访问的类中。简而言之,这是控制器的责任。这是我最终使用的解决方案。Anton Semenov是正确的:MVC的思想要求开发人员在控制器中而不是在视图中设置默认值。我想将日期时间值(dd/MM/yyyy-MM:HH:ss)的格式更改为(yyyy/MM/dd),在使用HtmlHelper
方法时从不设置value属性。在将模型传递给视图之前,在GET方法中设置属性值。为什么会这样?为什么我要在VM中硬编码属性值?这对我来说毫无意义,但也许我想做些不同的事情?我试图将查询字符串参数id传递给Create视图,并用该id值填充editorforhelper,以便它将FK id保存在db中。我该怎么做呢?我想这里的答案不是使用DateTime.Now,而是使用@value属性来设置Html元素的值。