我的模特
public class Questions
{
public string Question { get; set; }
public string[] Options { get; set; }
}
我的控制器方法来创建纸张
[HttpPost]
public ActionResult Create()
{
var resolveRequest = HttpContext.Request;
resolveRequest.InputStream.Seek(0, SeekOrigin.Begin);
string jsonString = new StreamReader(resolveRequest.InputStream).ReadToEnd();
var dist = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(jsonString);
string[] result = dist.Select(kv => kv.Value.ToString()).ToArray();
var conn = new MongoClient(Settings.Default.ConnectionStringSetting);
var server = conn.GetServer();
mongodb = server.GetDatabase(Settings.Default.DbMongoName);
var collection = mongodb.GetCollection<MultiChoice>("MultiChoice");
collection.Update(Query<PaperDetail>.EQ(s => s.paperName, "sam"), Update<MultiChoice>.AddToSet(s => s.questions, new Questions { Question = "Some question", Options = result }));
return RedirectToAction("MultipleChoice");
}
我想将我的json字符串转换为字符串数组并将所有数据传递给&#34; string []选项&#34;
答案 0 :(得分:0)
您尝试使用类似代码生成多项选择然后尝试将json字符串转换回来的方式,而不是ASP.Net MVC
或WebAPI
希望您这样做。有非常简单易行的方法。最好的方法是 -
试试这篇文章 -
https://msdn.microsoft.com/en-us/library/dd405231(v=vs.98).aspx
编辑:
使用类似的东西 -
[HttpPost]
public ActionResult Create(Questions model)
{
}
但为了完成这项工作,您必须使用类似于此的视图 -
@model Questions
......
@using (Html.BeginForm("Create", ..., FormMethod.Post,..)
{
@Html.TextBoxFor(x => x.Question)
@Html.DropDownListFor(model => model.Options, Model.Options.Select(x => new SelectListItem() { Text = x Value = x }))