Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVC json保存文件_Json_Visual Studio 2010_Visual Studio 2012_Model View Controller_Visual Studio 2013 - Fatal编程技术网

MVC json保存文件

MVC json保存文件,json,visual-studio-2010,visual-studio-2012,model-view-controller,visual-studio-2013,Json,Visual Studio 2010,Visual Studio 2012,Model View Controller,Visual Studio 2013,我得到了一个带有文本框和提交按钮的视图 当我按下submit时,我希望文本框的数据在json文件中(没有数据库) 现在,当我在提交其他数据时再次使用PSU时,它仅适用于save 1data命令 然后重写我的json文件,我如何保存它 控制器 public class HomeController : Controller { public ActionResult Index() { // jsonkalender.Add(new

我得到了一个带有文本框和提交按钮的视图 当我按下submit时,我希望文本框的数据在json文件中(没有数据库)

现在,当我在提交其他数据时再次使用PSU时,它仅适用于save 1data命令 然后重写我的json文件,我如何保存它

控制器

 public class HomeController : Controller
 {
        public ActionResult Index()
        {
            //   jsonkalender.Add(new kalender() { Password = "" });
            //kalender kl = new kalender();
            //kl.Password = obj.Password;
            //kl.MyEnum = TestEnum.taak;
            //obj.Password = kl.Password;
            //Load(null);
            return View();
        }
        private const string JsonFileName1 = @"C:\\Users\Alexander\Desktop\times1.json";
        private List<kalender> _times;

        [HttpPost]
        public ActionResult Index(kalender obj)
        {
          //  Load(obj);
          //  _times.Add(new kalender() { Datum = DateTime.Now.ToString() });
            kalender kl = new kalender();
           Json(new { datum = obj.Datum,wachtwoord = obj.Password });
           // _times.Add(obj);

            try
            {

            }
            catch (Exception) { }

            obj.SelectedEnumId++;

          //  kl.Password = obj.Password;
            var json = JsonConvert.SerializeObject(obj, Formatting.Indented);


            using (var writer = new StreamWriter(JsonFileName1))
            {
                writer.Write(json);

            }
            return View();
        }    

        private void Load(kalender obj)
        {
            if (System.IO.File.Exists(JsonFileName1))
            {
                using (var reader = new StreamReader(JsonFileName1))
                {
                    var json = reader.ReadToEnd();
                    obj = JsonConvert.DeserializeObject<kalender>(json);
                }
            }

            if (obj == null)
                obj = new kalender();
        }
     }
}
我的输出错误,如何更改

{ “基准”:“2014年10月28日”, “密码”:“hrth”, “类型”:空, “SelectedEmid”:0, “MyEnum”:0, “rand1”:0 }{ “基准”:“2014年10月28日”, “密码”:“hrth”, “类型”:空, “SelectedEmid”:0, “MyEnum”:0, “rand1”:0 }

这是我的json文件,但我需要这个 [ { “基准”:“2014年10月28日”, “密码”:“hrth”, “类型”:空, “SelectedEmid”:0, “MyEnum”:0, “rand1”:0 },{ “基准”:“2014年10月28日”, “密码”:“hrth”, “类型”:空, “SelectedEmid”:0, “MyEnum”:0, “rand1”:0 }
]

对附加到文件的SteamWriter对象使用重载,而不是覆盖文件


注意StreamWriter构造函数中添加了
bool
true

ye感谢它的工作,但是现在我得到了错误“Datum”:“10/01/2014”,“Password”:“zfz”,“Type”:null,“selectedemid”:1,“MyEnum”:0}{“Datum”:“10/01/2014”,“Password”:“zfz”,“Type”:null,“selectedemid”:1,“MyEnum”:0}谢谢,但是我的输出文件是错误的,我怎么能延迟它与这个标签一起工作[]现在我不能用deralize和datatalbes读入它,因为文件格式不好,我不能告诉你。我建议您研究一些JSON和更多的通用编码。我所有的答案都建议只附加到文件中,而不是覆盖它
 public class kalender
 {
     //public string Datum { get; set; }
     //  [Required]  
     //  [DataType(DataType.Date)]
     //  public Nullable<System.DateTime> Datum { get; set; }

     public string Datum { get; set; }    
     [Required]    
     public string Password { get; set; }
     [Required]  
     public string Type { get; set; }
     [Required]
     [Key]
     public int SelectedEnumId { get; set; }
     [Required]  
     public TestEnum MyEnum { get; set; }
    }
}
@model PE1.Models.kalender
@{
    ViewBag.Title = "Index";
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
}


@{
    AjaxOptions options = new AjaxOptions();
    options.HttpMethod = "POST";
    options.Confirm = "Zeker?";
    options.OnBegin = "OnBegin";
    options.OnComplete = "OnComplete";
    options.OnFailure = "OnFailure";
    options.OnSuccess = "OnSuccess";
    options.LoadingElementId = "divProgress";
    options.LoadingElementDuration = 1000;
    options.UpdateTargetId = "divResponse";
    options.InsertionMode = InsertionMode.InsertAfter;

}
<script type="text/javascript">
    $(function () { // will trigger when the document is ready
        $('.datepicker').datepicker(); //Initialise any date pickers

    });
</script>
    <div>
       <h1>Online Kalender</h1> 
        <div id="kader">
      <p>THuis</p>
@using (Html.BeginForm("Index", "Home", options))
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Kaldender</legend>
        @*@Html.DropDownListFor(model => model.Password, listItems)*@<br />
        @Html.LabelFor(model => model.Password)<br />

        @Html.EditorFor(model => model.Password)

        @Html.ValidationMessageFor(model => model.Password)<br />
        @Html.EnumDropDownListFor(model => model.MyEnum)

        <div class="form-group input-group-sm">
            @Html.LabelFor(model => model.Datum)
            @Html.TextBoxFor(model => model.Datum, new { @class = "form-control datepicker", placeholder = "Enter Drop-off date here..." })
            @Html.ValidationMessageFor(model => model.Datum)
        </div>
        <input type="submit" value="Submit"/>
        <input type="reset" value="Reset"/>
    </fieldset>
    @Ajax.ActionLink("Klik hier om een Ajax ActionLink uit te voeren", "ProcessLink", options)
}
public enum TestEnum : int
{
    taak,
    vergadering
}
using (var writer = new StreamWriter(JsonFileName1, true))
{
    writer.Write(json);

}