How do I add placeholder text from the model into a MVC view?

后端 未结 9 995
余生分开走
余生分开走 2021-02-05 12:43

I have a model:

[DataType(DataType.EmailAddress)]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[Display(Prompt = \"Email Address\")]
public string Email { ge         


        
相关标签:
9条回答
  • 2021-02-05 13:05

    In your controller method that renders view say

    ViewData["Name"] = "blabbity blah";

    then

    @Html.TextBoxFor(u => u.Company, new { @placeholder = @ViewData["Name"] })

    Actually better yet you can do this.

     public ActionResult Index()
        {              
              NewLogin n = new ClassModel().PopModel();
              n.Company = "fsdfsdf";
    
              return View(n);
        }
    

    @Html.TextBoxFor(u => u.Company, new { @placeholder = Model.Company })

    0 讨论(0)
  • 2021-02-05 13:13
    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" , @placeholder = "Username" })
    
    0 讨论(0)
  • 2021-02-05 13:20

    A little late, but if someone is still looking for it...

    @Html.EditorFor(model => model.Email, 
    new { htmlAttributes = new { @class = "form-control input-md",
    @placeholder = "Whatever you want as a placeholder" } })
    

    It's perfect and clean!

    0 讨论(0)
  • 2021-02-05 13:23

    Check out this answers to this question, which has been answered aleady (a few times).

    • Html attributes for EditorFor() in ASP.NET MVC
    • ASP.NET MVC 3 Razor - Adding class to EditorFor

    @Html.EditorFor uses templates, and so the approach to this problem is using a customised template.

    0 讨论(0)
  • 2021-02-05 13:24

    This solved my issue:

    @Html.EditorFor(model => model.Email, new { htmlAttributes = 
    new { @class = "form-control input-sm", 
    placeholder = @Html.DisplayNameFor(m=>m.Email) } })
    

    The code that did it was

    placeholder = @Html.DisplayNameFor(m=>m.Email) 
    
    0 讨论(0)
  • 2021-02-05 13:25

    you can use it as follows for the date which will also give you a "datetime" picker.

    [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}",ApplyFormatInEditMode =true)]
    
    0 讨论(0)
提交回复
热议问题