将ASP.Net中的textBoxes与变量绑定

时间:2013-11-23 18:58:15

标签: c# asp.net

我试图将html输入文本框

绑定在一起
<input type="text" id="serviceName" value="@serviceName" />

变量位于我的cshtml文件顶部

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

但是当我在sql命令上设置断点时,serviceName应该有值,它仍然是null。

我是否知道如何将textBox serviceName中的文本绑定到该变量?

感谢您的时间。

@{

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);

    }
}

这是我的页面顶部现在的html部分:

  <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" value=@Request.Form["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>

        }  

           仍然当我使用serviceName的断点值进行调试时,我仍然遇到绑定它的问题。

由Dylan Corriveau编辑正确的解决方案:

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

This网站表示,如果要上传文件,则需要使用此类型的编码。

1 个答案:

答案 0 :(得分:1)

如果我没记错的话,我相信Request.Form会接受输入的名称,而不是ID。所以在这种情况下,就是这样:

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

如果这不是问题,我想了解更多信息......你在使用MVC吗?的WebForms?你是如何发送表格的?

编辑: 我太愚蠢。 @serviceName是你列出的值对吗?你需要放一个名字元素。例。在您的输入中,试试这个

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

然后在你的代码中,就像你之前那样做

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

正如我所提到的,每个输入也应该有一个名称(你应该能够复制并过去那个......)

  <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>