0

I have tried to bind together html input textBox

<input type="text" id="serviceName" value="@serviceName" />

with variable on the top of my cshtml file

var serviceName = Request.Form["serviceName"];

But when I set breakpoint on sql command where should serviceName have value it is still null.

May I know how to bind text in textBox serviceName to that variable?

Thank you for your time.

@{

Layout = "~/_SiteLayout.cshtml";
Page.Title = "Registrovaní uživatelé";





var serviceLat = Request.Form["serviceLat"];
var serviceLon = Request.Form["serviceLon"];
var serviceContact = Request.Form["serviceContact"];
var serviceDescription = Request.Form["serviceDescription"];
var serviceCrypto = Request.Form["serviceCrypto"];
var serviceLink = Request.Form["serviceLink"];
var serviceType = Request.Form["serviceType"];
var fileName = "";
var fileMime = "";
var serviceName = "";
var email = "";


var userId = Request.Form["userId"]; 
if (IsPost)
{
    var uploadedFile = Request.Files[0]; // on this line it fails
    fileName = Path.GetFileName(uploadedFile.FileName);
    if (fileName != String.Empty)
    {
        fileMime = uploadedFile.ContentType;
        var fileStream = uploadedFile.InputStream;
        var fileLength = uploadedFile.ContentLength;
        email = Request.Form["email"];
        serviceName = Request.Form["serviceName"];
        byte[] fileContent = new byte[fileLength];
        fileStream.Read(fileContent, 0, fileLength);
        var db1 = Database.Open("StarterSite");
        var sql = "UPDATE services SET FileName=@0, FileContent=@1, MimeType=@2,"+
           "IDTypeOfService=@4,IDCryptoCur=@5,GeoLong=@6,GeoLat=@7,DescriptionService=@8,LinkService=@9,TitleService=@10,ServiceContact=@11 " + "WHERE IDklient=@3; IF @@ROWCOUNT=0 INSERT INTO services " +
        "(IDklient,IDTypeOfService,IDCryptoCur,GeoLong,GeoLat,DescriptionService,LinkService,TitleService,ServiceContact,FileName,FileContent,MimeType)"+
        "values (@3,@4,@5,@6,@7,@8,@9,@10,@11,@0,@1,@2)";
       db1.Execute(sql, fileName, fileContent, fileMime, userId,serviceType,serviceCrypto,serviceLon,serviceLat,serviceDescription,serviceLink,serviceName,email);

    }
}

This is on the top of my page now html part:

  <section class="email">
        <form method="post">
<fieldset>
       <legend>Bez registrace</legend>
            <label for="email" @if(!ModelState.IsValidField("email")){<text>class="error-label"</text>}>Email address</label>
            <input type="text" id="emailFast" name="emailFast" value="@email" @Validation.For("email") />
            @* Write any email validation errors to the page *@
            @Html.ValidationMessage("email")
      <label for="email" >Název</label>
      <input type="text" id="serviceName" [email protected]["serviceName"] />
      <label for="email" >Zeměpisná šířka</label>
      <input type="text" id="serviceLat" value="serviceLat"/>
      <label for="email" >Zeměpisná délka</label>
      <input type="text" id="serviceLon" value="serviceLon"/>
      <label for="email" >Kategorie</label>
      <input type="text" id="serviceType" value="serviceType"/>
      <label for="email" >Kontakt</label>
      <input type="text" id="serviceContact" value="serviceContact" />
      <label for="email" >Stručný popis</label>
      <input type="text" id="serviceDescription" value="serviceDescription" />
      <label for="email" >Krypto měna</label>
      <input type="text" id="serviceCrypto" value="serviceCrypto" />
      <label for="email" >Webová stránka</label>
      <input type="text" id="serviceLink" value="serviceLink" />
      <label for="email" >Logo</label>
     </fieldset>

         @FileUpload.GetHtml(
            initialNumberOfFiles: 1,
            allowMoreFilesToBeAdded: false,
            includeFormTag: true,
            uploadText: "Upload")


        @if (IsPost && fileName != String.Empty)
        {
            <span>Obrázek byl nahrán!</span>

        }  

Still when I do the debug with breakpoint value for serviceName is null I still have problems with binding it.

Edit of the correct solution by Dylan Corriveau:

    <form method="post" enctype ="multipart/form-data">

This sites says that it is neccessery to use this type of encoding if you are uploading a file.

8
  • I want to confirm something. Are your other variables null too? Or is it just the serviceName? Commented Nov 23, 2013 at 19:59
  • <input type="text" id="serviceName" [email protected]["serviceName"] /> should be just value="serviceName". Right here, its saying "give me the value of serviceName, and place it in the value of serviceName", which doesn't exist, so it would be null Commented Nov 23, 2013 at 20:01
  • Other except the fileName, FileMime and every that is connected with uploading image. But the ones that should upload text from textboxes are null. Commented Nov 23, 2013 at 20:02
  • try adding a name to each, as I said in my new edit. You should be able to just copy and paste it. Let me know what happens Commented Nov 23, 2013 at 20:08
  • @DylanCorriveau I tried it just with serviceName but still null. I quiet don't get it. It is in IsPost it has a name binded to variable. Where can be the problem? By the way I think if the value="serviceName" is inserted that it only sets the default text of the textbox right? Commented Nov 23, 2013 at 20:09

1 Answer 1

1

If I remember correctly, I believe Request.Form takes in the Name of an input, not an ID. So in this case, it would be this:

var serviceName = Request.Form["@serviceName"];

If this isn't the issue, I would like to know a bit more information... Are you using MVC? WebForms? How are you sending your form over?

Edit: I'm silly. @serviceName is the value you listed right? You would need to put a name element. Example. In your input, try this

<input type="text" name="serviceName" value="@serviceName" />

Then in your code, just do it like you had it before

var serviceName = Request.Form["serviceName"];

As I mentioned, each input should also have a name (you should be able to just copy and past that....)

  <label for="email" >Název</label>
  <input type="text" id="serviceName" name="serviceName" value="serviceName" />
  <label for="email" >Zeměpisná šířka</label>
  <input type="text" id="serviceLat" name="serviceLat" value="serviceLat"/>
  <label for="email" >Zeměpisná délka</label>
  <input type="text" id="serviceLon" name="serviceLon"  value="serviceLon"/>
  <label for="email" >Kategorie</label>
  <input type="text" id="serviceType" name="serviceType"  value="serviceType"/>
  <label for="email" >Kontakt</label>
  <input type="text" id="serviceContact" name="serviceContact"  value="serviceContact" />
  <label for="email" >Stručný popis</label>
  <input type="text" id="serviceDescription" name="serviceDescription"  value="serviceDescription" />
  <label for="email" >Krypto měna</label>
  <input type="text" id="serviceCrypto" name="serviceCrypto"  value="serviceCrypto" />
  <label for="email" >Webová stránka</label>
  <input type="text" id="serviceLink" name="serviceLink"  value="serviceLink" />
  <label for="email" >Logo</label>
Sign up to request clarification or add additional context in comments.

5 Comments

thank you for your interest in helping me. I'm using Razor v2. But I'm not sure whether I use WebForms or not. When I click in Visual Studio 2012 File - New - Website - Installed - Templates - Visual c# I choose ASP.NET Web Site (Razor v2) it is some kind of template from Microsoft. So that is what I choosen.
I'm guessing you tried the name i mentioned above? Also, not a problem! Also, a way to tell, do you have a controllers folder in your project? If so, that's probably MVC, if not, its WebForms
I tried @serviceName but the variable is still null. Well there isn't located any folder called controllers in the project solution. I have only: Account,App_Code,App_Data,bin,Content,packages,Scripts folders.
try my edit, also, that file structure sounds like WebForms as a general note. If you do get more questions in the future, try including that tag as well
thank you for your time but still it haven't changed anything. Can you please see the code edited in my question? Thank you so much.

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.