Mustafa Sattar
Mustafa Sattar

Reputation: 161

How can i set the html id attribute in razor

I want to configure datepicker in my MVC 5 form. I have included all the reference libraries like Jquery, datepicker etc.

Following is the line from MVC razor view, which is created by Visual Studio in a result for MVC 5 scaffolding.

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

any help in this regard

Upvotes: 4

Views: 21521

Answers (2)

David Silva-Barrera
David Silva-Barrera

Reputation: 1136

I just tested this solution with MVC5 .net 4.5.1. I was trying to set manually the form Id because I was going to have several forms in the same page and I needed to control then separatelly with JS :

@using (Ajax.BeginForm("Edit", "Products", null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divEmp"+Model.Id }, htmlAttributes : new { id = "form_" + Model.Id } ))
{

}

Enfasis in last parameter: htmlAttributes : new { id = "form_" + Model.Id }

It is the same with @Html.BeginForm() and other helpers.

The htmlAttributes: part is not really necessary.

Upvotes: 0

Tobias
Tobias

Reputation: 2985

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

Look at this post: How can I set id using Html.EditorFor with MVC3

 @Html.EditorFor(model => model.News_Date, new { htmlAttributes = new { @class = "form-control", id = "my_custom_id" }})

Upvotes: 5

Related Questions