Bohdi
Bohdi

Reputation: 1305

cshtml c# not listening to assigned id for some reason?

So I have a basic CRUD I am working on, and Im trying to get Jquery validation working on it. I have it almost all set up except I need to give my form an id. I am using cshtml in visual studio and try to assign the id to the form using the following code:

@using (Html.BeginForm( new { id = "daftform" })) {
@Html.ValidationSummary(true)
<fieldset>
<legend>News</legend> 

However the generated html looks like this:

<form action="/News/Create/daftform" method="post">    <fieldset>
    <legend>News</legend>

I am pretty sure this is how to assign an id to an element as I use this method to assign classes in a similar way. Can anyone tell me where im going wrong?

I just want it to assign 'daftform' as an id not as an action.

Sorry if its a simple answer, fairly new to c#.

Upvotes: 0

Views: 247

Answers (2)

sander
sander

Reputation: 749

@using (Html.BeginForm(,"actionname","controllername",formmethod.post/get, new {id = "yourId"})

is the way to go

Upvotes: 0

Shyju
Shyju

Reputation: 218952

Use this overload

public static MvcForm BeginForm(
    this HtmlHelper htmlHelper,
    string actionName,
    string controllerName,
    FormMethod method,
    Object htmlAttributes
)

So your code can be changed to

@using (Html.BeginForm("Create","News",FormMethod.Post, new { id = "daftform" }))
{
  //form elements
}

This will create a form tag with Id property set to "daftform"

Upvotes: 3

Related Questions