Necessary input in Html.TextBoxFor

Is there a way to format the value of a TextBox using the following structure:

<td>@Html.TextBoxFor(m =>(m.Code), new { @required  = "required"})</td>

Initially, this format works as intended. However, when a default value is set for the TextBox:

<td>@Html.TextBoxFor(m =>(m.Code), new { @Value = @Model.Code, @required  = "required"})</td>

An issue arises where an empty value is accepted despite the generation of the following Html code:

<td><input id="Code" name="Code" required="required" type="text" value="fff       "></td>
  1. What could be causing this problem?
  2. Are there any solutions to address this issue?

Answer №1

For some reason, I discovered that deleting the space between = and @ fixes the issue.

<td>@Html.TextBoxFor(m =>(m.Code), new { @Value <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="764b363b1912131a5835191213">[email protected]</a>, @required  = "required"})</td>

Strange but true!

Answer №2

Include the following code snippet to validate the code property:

@Html.ValidationMessageFor(m => m.Code)

To define the code property, use the following code:

[Required]
public string Code{ get; set; }

To set the value in the code Textbox from the controller:

Model.Code="fffff";

In the view, use the following code:

<td>@Html.TextBoxFor(m =>(m.Code), new { @Value = @Model.Code)</td>
@Html.ValidationMessageFor(m => m.Code)

Don't forget to include the validation JavaScript files:

<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>

Give it a try and see how it works!

Answer №3

Avoid using parentheses

@Html.TextBoxFor(m => m.Code, new { @class = "txtCode" })

By assigning the value to 'code' on the controller side, it will automatically populate the text box.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Broken link in navigation bar

I encountered an issue with the navigation bar on this website. When I click on "Leistungsspektrum" in the navigation bar, it returns a message stating that The requested URL /leistunsspektrum.html was not found on this server. However, the actual file nam ...

Creating a background overlay on top of an image background using styled-components in React: A step-by-step guide

I need help figuring out how to construct a unique component in react styled component that combines a background image with a black semi-transparent overlay. For visual reference, here is an example: https://i.sstatic.net/R6IBY.jpg ...

RequireJS in use with .NET Angular

I set up a fresh ASP.NET Empty Web Application using Visual Studios 2013. Within this project, I have an angular application organized in the following directory structure: ├───TestProject │ ├───index.html │ ├───package.jso ...

What is causing this error to appear in Next.js? The <link rel=preload> is showing an invalid value for `imagesrcset`

I've got a carousel displaying images: <Image src={`http://ticket-t01.s3.eu-central-1.amazonaws.com/${props[organizationId].events[programId].imgId}_0.cover.jpg`} className={styles.carouselImage} layout="responsive" width={865} ...

Adjusting the amount of rows and columns in a fluid manner with CSS grid (updated)

After conducting my research, it seems that the most effective way to set the final value of a CSS grid is either by directly specifying it or by creating/manipulating the css :root value. A similar query was previously raised here, although the answers p ...

Guidelines for implementing drag and drop functionality for my specific scenario

Looking to create a restricted area for dragging elements within my app. Specifically, I want the element to only be draggable within a certain defined space. Here is what I currently have: http://jsfiddle.net/L73T5/10/ html <div id='background ...

Can you please explain the meaning of this wildcard symbol?

We recently came across a part of our grunt file that was written by a former colleague. While the code functions correctly, the specific purpose of one segment remains a mystery to us! Here is the enigmatic code snippet: dist: { files: [{ ex ...

Building Web Services and communication protocols with Windows Communication Foundation in Visual Studio 201

As a newcomer to WCF programming, I have been diligently following a series of Getting Started tutorials from the link provided below: http://msdn.microsoft.com/en-us/library/ms734712.aspx After successfully hosting the service in a console application, ...

Expand the video comparison slider to occupy the entire width and height

I am striving to develop a video comparison slider that fills the height and width of the viewport, inspired by the techniques discussed in this informative article: Article Despite my efforts, I have not been successful in achieving this effect so far a ...

Tips for maintaining a website within a 960px width while stretching the background on the x-axis

I have enclosed the entire website within a div with the ID container. The CSS styling for the container is as follows: #container { width: 960px; background: #FFFFFF; margin: 0 auto; border: 1px solid #000000; text-align: left; } How ...

Alignment in the vertical direction of two lines

I've been struggling to align some HTML elements visually the way I want. There are two lines of text followed by a link. I'm trying to get the link to be vertically centered between the two lines, rather than just after the second line. Using a ...

Opacity in text shadow effect using CSS

Within my text lies the following css snippet: .text-description-header { font-size: 250%; color: white; text-shadow: 0 1px 0 black; } Observe the dark shadow effect it possesses. Inquiry I am curious if there is a way to alter the shadow t ...

The appearance of CSS output is noticeably distinct between iPhones and personal computers

My issue is with the development of my website. When I access it on my PC, everything looks fine. However, when I try to visit the site on my iPhone, there is a noticeable difference in the output. The space above the navigation bar appears differently whe ...

What is the best way to align these Iframes in the center?

This is the html <div class="main_content"> <section class="episodio"> <article class="contenedor_episodios"> <h2>Episodios</h2> <div class="episodio_spotify" ...

Retain the jQuery dropdown menu in an open state while navigating to a different webpage on the

I am encountering an issue with a drop-down menu on my website. Whenever I click on a submenu link, the new page opens but the menu automatically closes. However, I want the active menu to remain open even on the new page. To solve this problem, I believe ...

Are you ready to create a Modal Factory?

For a while now, I have been utilizing modals in various front-end frameworks to communicate with users in my applications. Typically, the process involves defining the modal's html and then rendering it through a click event. As my apps continue to ...

What methods does the W3C CSS Validator use to recognize a CSS3 document?

Help Needed! Can someone tell me how to specify a CSS3 file for validation by the W3C? In HTML5, we use <!DOCTYPE html>, but what should we use in a CSS file? Whenever I try to validate my CSS file containing CSS3 elements like @font-face and box- ...

Problems with CSS animations on mobile devices in WordPress

On my website powered by Elementor Pro, I have implemented custom CSS to animate the Shape Divider with a "Brush Wave" style. Below is the code snippet: .elementor-shape-bottom .elementor-shape-top .elementor-shape body { overflow-x:hidden; } @keyframes ...

What is the best way to set breakpoints on Material UI components?

Currently, I am exploring material-ui and I'm curious if there is a way to set all breakpoints at once. The code snippet below seems quite repetitive. Is there a more efficient method to achieve this? <Grid xs={12} sm={12} md={12} lg={12} xl={12} ...

Customize the background image in your Windows 8 app developed with HTML

While working on my app with the grid app template, I placed an image in the images folder. However, when I attempted to change the background image, it didn't seem to take effect. This is the code snippet that I used: HMTL <div id="contenthost" ...