为什么这个var REQUEST [" name"]方法总是返回null?

时间:2014-05-21 04:49:30

标签: asp.net razor null request webmatrix

我正在自学习如何创建表单,将响应传递给单独的处理程序,并通过电子邮件将其发送到我的网站帐户。我有形式,有效,奇怪的是,我有邮件部分工作。但是,这个例子几乎直接来自微软的一些ASP.NET帮助页面 - 但它不起作用,我似乎无法找出原因。什么错了,应该怎么读呢?

这是一些表单代码: {     

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <form action="Mailform.cshtml" enctype="text/plain" 
        method="post" name="formcontent">
        <ul> 
            <li>
                <label for="name" class="heavyred">Contact Name: &nbsp</label>
                <input type="text"  name="contact" autofocus="true" size="40"
                    required="" /><span class="tinyfont">&nbsp red denotes a 
                    required field</span>
                <span class="italicfont"style="margin-left: 22%"><br>Person 
                    overseeing site development</span>
            </li>
            <li>
                <label for="strngweak">List strengths:<br></label>
                <textarea name="strngweak"  placeholder="420 chars max" 
                     cols="60" rows="7"></textarea>                    
            </li>
        </ul>
        <div>
            <input class="hyper buttn" style="margin-right:1%; font-size: 1em;
                "type="submit" value="SUBMIT">
        </div>     
    </form>        
</body>

}

handler.cshtml代码:(前两个请求最终为NULL而不是按名称获取数据)

{

@{

 var contact = Request ["contact"];  <----- NULL instead of name=contact data
 var stweak = Request ["strngweak"]; <----- NULL instead of name=strngweak data

 var from = "info@portalmagician.com";
 var to = "sirrobcop@yahoo.com"; 
 var subject = "I WANT A WEBSITE";
 var msg = "From: ";            
 var errorMessage = "";
 var debuggingFlag = true;

 msg += contact + " Strengths: " + stweak;

 }

{ try-catch block initializes webmail helper and sends email here }

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    @if (IsPost) {

        if (errorMessage == "") {<p>Your message has been sent!</p>;}

        else {<p>Unable to send due server down or bad server info.</p>;}
    }        
</body>

}

1 个答案:

答案 0 :(得分:1)

将表单的enctype修改为application/x-www-form-urlencoded(或删除它)。