简体   繁体   English

Html.EditorFor 设置默认值

[英]Html.EditorFor Set Default Value

Rookie question.菜鸟问题。 I have a parameter being passed to a create view.我有一个参数传递给创建视图。 I need to set a field name with a default value.我需要设置一个具有默认值的字段名称。 @Html.EditorFor(model => model.Id) I need to set this input field with name Id with a default value that is being passed to the view via an actionlink. @Html.EditorFor(model => model.Id) 我需要使用名称 Id 设置此输入字段,并使用通过操作链接传递给视图的默认值。

So, how can this input field --@Html.EditorFor(model => model.Id) -- get set with a default value.那么,这个输入字段 --@Html.EditorFor(model => model.Id) 如何设置为默认值。

Would the following work??以下会工作吗? Where the number 5 is a parameter I pass into the text field to set default value.其中数字 5 是我传递到文本字段以设置默认值的参数。

@Html.EditorFor(c => c.PropertyName, new { text = "5"; })

Here's what I've found:这是我发现的:

@Html.TextBoxFor(c => c.Propertyname, new { @Value = "5" })

works with a capital V, not a lower case v (the assumption being value is a keyword used in setters typically) Lower vs upper value使用大写的 V,而不是小写的 v(假设 value 是通常在 setter 中使用的关键字) Lower vs upper value

@Html.EditorFor(c => c.Propertyname, new { @Value = "5" })

does not work不起作用

Your code ends up looking like this though你的代码最终看起来像这样

<input Value="5" id="Propertyname" name="Propertyname" type="text" value="" />

Value vs. value.价值与价值。 Not sure I'd be too fond of that.不确定我会不会太喜欢那个。

Why not just check in the controller action if the proprety has a value or not and if it doesn't just set it there in your view model to your defaulted value and let it bind so as to avoid all this monkey work in the view?如果属性是否有值,为什么不直接检查控制器动作,如果它不只是在您的视图模型中将它设置为您的默认值并让它绑定以避免所有这些猴子在视图中工作?

The clean way to do so is to pass a new instance of the created entity through the controller:这样做的干净方法是通过控制器传递已创建实体的新实例:

//GET
public ActionResult CreateNewMyEntity(string default_value)
{
    MyEntity newMyEntity = new MyEntity();
    newMyEntity._propertyValue = default_value;

    return View(newMyEntity);
}

If you want to pass the default value through ActionLink如果要通过 ActionLink 传递默认值

@Html.ActionLink("Create New", "CreateNewMyEntity", new { default_value = "5" })

Its not right to set default value in View.在 View 中设置默认值是不对的。 The View should perform display work, not more.视图应该执行显示工作,而不是更多。 This action breaks ideology of MVC pattern.这个动作打破了 MVC 模式的意识形态。 So the right place to set defaults - create method of controller class.所以设置默认值的正确位置 - 控制器类的创建方法。

Better option is to do this in your view model like更好的选择是在您的视图模型中执行此操作,例如

public class MyVM
{
   int _propertyValue = 5;//set Default Value here
   public int PropertyName{
       get
       {
          return _propertyValue;   
       }
       set
       {
           _propertyValue = value;
       }
   }
}

Then in your view那么在你看来

@Html.EditorFor(c => c.PropertyName)

will work the way u want it (if no value default value will be there)将按照你想要的方式工作(如果没有值默认值将在那里)

I just did this (Shadi's first answer) and it works a treat:我只是这样做了(Shadi 的第一个答案)并且它很有效:

    public ActionResult Create()
    {
        Article article = new Article();
        article.Active = true;
        article.DatePublished = DateTime.Now;
        ViewData.Model = article;
        return View();
    } 

I could put the default values in my model like a propper MVC addict: (I'm using Entity Framework)我可以像一个适当的 MVC 瘾君子一样将默认值放在我的模型中:(我正在使用实体框架)

    public partial class Article
    {
        public Article()
        {
            Active = true;
            DatePublished = Datetime.Now;
        }
    }

    public ActionResult Create()
    {
        Article article = new Article();
        ViewData.Model = article;
        return View();
    } 

Can anyone see any downsides to this?任何人都可以看到这有什么缺点吗?

Shouldn't the @Html.EditorFor() make use of the Attributes you put in your model? @Html.EditorFor()不应该使用您放在模型中的属性吗?

[DefaultValue(false)]
public bool TestAccount { get; set; }

Shove it in the ViewBag :把它ViewBag

Controller:控制器:

ViewBag.ProductId = 1;

View:看法:

@Html.TextBoxFor(c => c.Propertyname, new {@Value = ViewBag.ProductId})

This worked for me这对我有用

In Controlle在控制中

 ViewBag.AAA = default_Value ;

In View在视图中

@Html.EditorFor(model => model.AAA, new { htmlAttributes = new { @Value = ViewBag.AAA } }

This worked for me:这对我有用:

In the controller在控制器中

*ViewBag.DefaultValue= "Default Value";*

In the View在视图中

*@Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter a Value", @Value = ViewBag.DefaultValue} })*

For me I need to set current date and time as default value this solved my issue in View add this code :对我来说,我需要将当前日期和时间设置为默认值,这解决了我在视图中的问题添加以下代码:

<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>

In the constructor method of your model class set the default value whatever you want.在您的模型类的构造函数方法中,您可以随意设置默认值。 Then in your first action create an instance of the model and pass it to your view.然后在您的第一个操作中创建模型的一个实例并将其传递给您的视图。

    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);
    }

This is my working code:这是我的工作代码:

@Html.EditorFor(model => model.PropertyName, new { htmlAttributes = new { @class = "form-control", @Value = "123" } })

my difference with other answers is using Value inside the htmlAttributes array我与其他答案的不同之处在于在 htmlAttributes 数组中使用 Value

Instead of using controller or html helper.You can also use Jquery to set default value to model attribute.而不是使用 controller 或 html 助手。您还可以使用 Jquery 将默认值设置为 model 属性。

   $('#PropertyName').val(15);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM