我做了约20分钟的浏览,无法弄清楚这一点。 如何获取POST到我的网址的数据?
我需要FormCollection参数吗?
是否有一个很好的替代PHP使用的print_r()或vardump()函数?或者我只是依赖调试器中的断点?
[IsLogged]
[HttpPost]
public JsonResult SaveRecord(FormCollection formCollection)
{
var disease = Convert.ToString(formCollection.GetValue("disease"));
var link = Convert.ToString(formCollection.GetValue("link"));
disease = Server.HtmlEncode(disease);
link = Server.HtmlEncode(link);
string[] output = new string[] { disease, link };
return Json(output);
}
- 编辑:客户端代码 -
<form method="post" action="@Url.Action("SaveRecord", "Dashboard")">
<div class="control-group">
<label class="control-label">Disease</label>
<div class="controls">
<textarea name="disease" placeholder="Insert the details of your disease."></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label">Remedy Link</label>
<div class="controls">
<input type="text" name="link" placeholder="Provide the link to your remedy." />
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="submit" value="Create Record" class="btn btn-primary" />
</div>
</div>
</form>
悬崖注意:我只有几天新MVC3所以很好:)这种语言比我习惯的语言更先进,但它非常甜蜜。
这是我的最终解决方案!
[IsLogged]
[HttpPost]
public JsonResult SaveRecord()
{
var disease = Convert.ToString(Request["disease"]);
var link = Convert.ToString(Request["link"]);
disease = Server.HtmlEncode(disease);
link = Server.HtmlEncode(link);
string[] output = new string[] { disease, link };
return Json(output);
}
答案 0 :(得分:3)
将强类型视图与模型一起使用:
@model YourModel
并在您的操作中为您的模型添加参数:
public JsonResult SaveRecord(YourModel model)
有关完整示例,请参阅http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model。
答案 1 :(得分:2)
我最近才问自己。
你需要的是Request.Form
,但它只显示变量名,而不是值,因为它是NameValueCollection。为了让它在Json输出上显示,你需要将它转换为Dictionary。
/// <summary>
/// Serializes object to JSON format
/// </summary>
/// <param name="input">Object</param>
/// <returns>JSON string</returns>
public static string SerializeObject(Object input, bool debug = true)
{
string json = JsonConvert.SerializeObject(input, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, MaxDepth = 2 });
if (debug)
{
return "<pre>" + json + "</pre>"; // In order to preview in browser
}
return json;
}
/// <summary>
/// Serializes Request.Form (NameValueCollection) to JSON format
/// </summary>
/// <param name="input">NameValueCollection</param>
/// <returns>JSON string</returns>
public static string SerializePostRequest(NameValueCollection post)
{
var dict = new Dictionary<string, string>();
foreach (string key in post.Keys)
{
dict.Add(key, post[key]);
}
return SerializeObject(dict);
}
[HttpPost]
public JsonResult SaveRecord(FormCollection formCollection)
{
// Same as print_r($_POST) in php
return Content(SerializePostRequest(Request.Form));
}
答案 2 :(得分:2)
jrummell是对的。您需要使用以下定义创建强类型视图和包含与表单输入的name属性匹配的属性名称的模型:
public class YourModel
{
public string Disease { get; set; }
public string Link { get; set; }
}
执行此操作后,模型绑定器将根据值提供程序提供的结果创建强类型模型。这是a link with more info。
值提供程序只是诸如RouteValueDictionary,Request.Form,Request.Files,Request.QueryString等对象的包装器.MVC人员巧妙地抽象出这些东西,所以你不必直接处理它。模型绑定器接收这些值并使用它来填充模型。