0

In my view I have the following, typical razor markup.

<div class="form-item-container">
  @Html.LabelFor(m => m.BookingFormFields.FirstName, new { @class = "form-item-label" })
  @Html.TextBoxFor(m => m.BookingFormFields.FirstName, new { @class = "form-item", name="fn" })
  @Html.ValidationMessageFor(m => m.BookingFormFields.FirstName, null, new { @class = "error-text" })
</div>

The problem is this results in querystring like this

?BookingFormFields.FirstName=Test

Which is fine and works, but wouldn't it be better if it could avoid exposing internal fields and be more succinct?

Wouldn't it look better if it looked more like this

?fn=Test

I could achieve this by intercepting the submit action in JavaScript, but I would like to avoid using that if there's a better native DotNet method that I'm not aware of!

Any ideas?

1

1 Answer 1

1

The problem is this results in querystring like this

?BookingFormFields.FirstName=Test

Your codes failed to override the name property of input

You could try

@Html.EditorFor(m => m.BookingFormFields.FirstName, null, "fn")

Result:

enter image description here

To introduce class, you may try:

@Html.EditorFor(m => m.BookingFormFields.FirstName, null, "fn", new { htmlAttributes = new { @class = "myCssClass", style = "Width:50px" } })

Result: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this does actually provide the desired result. I do need to work out how to re-introduce the css class though.
Hi,@Username_null,I've updated my answer,hopes help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.