CSS is functioning properly with HTML, but it seems to be having trouble with Razor

Creating a contact form initially as pure html:

<div id="contact-container">
<form class="cf">
    <div class="half left cf">
        <input type="text" id="input-name" placeholder="Name">
        <input type="email" id="input-email" placeholder="Email address">
        <input type="tel" id="input-phone" placeholder="Phone">
        <input type="text" id="input-subject" placeholder="Subject">
    </div>
    <div class="half right cf">
        <textarea name="message" type="text" id="input-message" placeholder="Message"></textarea>
    </div>
    <input type="submit" value="Submit" id="input-submit">
</form>
</div>

However, converting to Razor syntax resulted in:

<div id="contact-container">
@Html.BeginForm("Contact", "Service", FormMethod.Post, new { @class = "cf" })
{
    <div class="half left cf">
        @Html.TextBoxFor(m => m.Name, new { htmlattributes = new { id = "input-name", placeholder = "Name" }})
        @Html.TextBoxFor(m => m.Email, new { htmlattributes = new { id = "input-email", placeholder = "Email Address" }})
        @Html.TextBoxFor(m => m.Phone, new { htmlattributes = new { id = "input-phone", placeholder = "Phone" }})
        @Html.TextBoxFor(m => m.Subject, new { htmlattributes = new { id = "input-subject", placeholder = "Subject" }})
    </div>
    <div class="half right cf">
        @Html.TextBoxFor(m => m.Message, new { htmlattributes = new { id = "input-message", placeholder = "Message" }})
    </div>
    <input type="submit" value="Submit" id="input-submit">
}
</div>

The converted version appears with unexpected text like System.Web.Mvc.Html.MvcForm { and excessive semicolons on the webpage. What could be causing this discrepancy?

Answer №1

You should wrap the form creation code inside a using block, like this:

<div id="contact-container">
@using (Html.BeginForm("Contact", "Service", FormMethod.Post, new { @class = "cf" })
{
    <div class="half left cf">
        @Html.TextBoxFor(m => m.Name, new { htmlattributes = new { id = "input-name", placeholder = "Name" }})
        @Html.TextBoxFor(m => m.Email, new { htmlattributes = new { id = "input-email", placeholder = "Email Address" }})
        @Html.TextBoxFor(m => m.Phone, new { htmlattributes = new { id = "input-phone", placeholder = "Phone" }})
        @Html.TextBoxFor(m => m.Subject, new { htmlattributes = new { id = "input-subject", placeholder = "Subject" }})
    </div>
    <div class="half right cf">
        @Html.TextBoxFor(m => m.Message, new { htmlattributes = new { id = "input-message", placeholder = "Message" }})
    </div>
    <input type="submit" value="Submit" id="input-submit">
}
</div>

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

The Bootstrap dropdown menu unexpectedly appears in front of the panel located above

Having trouble with the Bootstrap dropdown panel. My web page has 4 main panels, each containing additional bootstrap panels. When I open one panel at a time, everything works fine. However, when I try to open multiple panels simultaneously, the positions ...

Does a pointer event match the 'click' event?

When it comes to mouse events, the 'click' event is triggered after both the mousedown and mouseup events occur. But with pointer events having a wider range of uses, I'm curious if there is an equivalent 'click' event for pointer ...

Putting off the execution of a setTimeout()

I'm encountering difficulties with a piece of asynchronous JavaScript code designed to fetch values from a database using ajax. The objective is to reload a page once a list has been populated. To achieve this, I attempted to embed the following code ...

Showing Information without NgFor Directives

I have successfully displayed data without using a ngFor loop. However, it is currently showing all the quote information from multiple customers. The CSS is arranged in such a way that the discount div is positioned next to the customerinfo div. Below is ...

Retrieve the values and IDs for every span element within the form

I have been attempting to retrieve all span elements within a form and make them editable input text fields. Once you click away, they should revert back to span elements. I will provide a live example in the attached fiddle. I tried my hand at it, but the ...

Issue with Canvas.doDataUrl not functioning properly in presence of an image on canvas

It seems like the code I have tried only works for local images. Can someone share a working code snippet for this? Here is what I've attempted: var base_image = new Image(); base_image.src = ("/img.png"); base_image.onload = function(){ var co ...

Issue: Nav component did not return any content during rendering. This typically indicates that a return statement is absent. To render nothing, you can return null

Encountering an error in my code: Error: Nav(...): Nothing was returned from render. This typically indicates a missing return statement or the need to return null. I am trying to create a styled components navbar but struggling to correct this issue... B ...

Switching image sources depending on CSS prefers-color-scheme: A guide

In my endeavor to incorporate the latest CSS prefers-color-scheme media query into my website, I encountered a challenge. I have an img (image) element featuring a very dark blue image, which clashes poorly with the new dark mode setting. Is there a simpl ...

Sticky positioning for dropdown menu in table column body

Greetings, I need some assistance with a formatting issue. My goal is to make the first and second columns of my table sticky by applying a "sticky" position to them. However, one of the columns contains a dropdown menu and the problem is that the content ...

Is there a way to modify the color scheme of my webpage while utilizing Bootstrap?

As I delve into programming with Bootstrap, I encountered an issue while attempting to change the background color of my body. Even though I used the following CSS code: body { background-color: #eba; width: 100%; height: 100%; } The color change ...

Adjust the transparency of an image preview using jQuery

Currently, I am working on creating a video gallery for a friend and I am experimenting with changing the thumbnail opacity using jQuery fade effect. While I can achieve this with CSS, I want to implement it with jQuery for a smoother transition. If you ...

When attempting to locate the clientWidth, the usage of absolute positioning can cause significant disruptions

When aiming to determine the clientWidth, certain code must be added. For example: #menuSystem a{ position: absolute; height: auto; width: auto; font-size: 15px; } To make it possible to calculate client width, the above code snippet must be included. Th ...

Elements generated from bootstrap's divs are not produced in a single line

I'm attempting to have a div append multiple other divs with the class "card" from Bootstrap, but instead of being created in a horizontal line, they are stacked on top of each other. I have tried to modify the "card" class by adding display: inline- ...

Utilizing itemprop as a CSS focus

After implementing the custom.css code provided below, I have successfully overridden the CSS files of a commercial Joomla 3 Template. div[itemprop="articleBody"] h1, h2, h3, h4, h5, h6 { margin-top: 18px!important; margin-bottom: 0px!important; ...

Looking for a way to enhance image resizing animations in JavaScript for a seamless visual

Having two images inside a container means not knowing their size in advance after uploading. The resize function used helps fit the images within the container, but the animation appears choppy. Is there a way to improve its smoothness? $(window).load(f ...

incorporating some margin to the left side of a text block

Is there a way to use margin-left:20px; to move a bullet point list on my webpage 20px inwards? <li>• Service level agreements to match your needs from next business day response to a two hour fix</li> <li>• 24x7x365 service coverage ...

Updating checkbox Icon in Yii2

Is there a way to customize the checkbox icon when it is checked in Yii2? Currently, I am adding a checkbox inside a div along with an icon: <i class="fa fa-check-circle icon-check-circle"></i>. However, the checkbox shows a default check symbo ...

Variability in the positioning of list item markers depending on the child's display property

Looking at this code: <ul> <li> <div id="container"> <button>toggle display</button> <div>text</div> <div>text</div> </div> </li> ...

Utilizing a local video file as a video background in HTML and CSS

I've hit a roadblock while attempting to set a video on my computer as the background. The video file is named "runbg.avi" and it resides in the same folder as my HTML and CSS files. After scouring through numerous websites, I encountered a couple of ...

Tips for updating multiple divs after submitting content with jQuery

My screen contains various widgets, each enclosed with different divs. Currently, I have a form that updates using AJAX when posted through jQuery. However, I am looking to refresh two additional divs located outside the form upon a single jQuery AJAX pos ...