开发者

How to update div tag in Javascript with data from model for onsubmit form asp.net mvc

In my page I have a form tag which submits to server, gets data and redirects to same page. The problem is the the div tag which has the data from server is not getting updated. How to do that in Javascript?

<% using (Html.BeginForm("Addfile", "uploadfile", FormMethod.Post, new
   {
       id = "uploadform",
       enctype = "multipart/form-data"
      
   }))
       { %>
<input type="file" id="addedFile" name="addedFile" /><br />
    <input type="submit" id="addfile" value="Addfile" />
    <div id="MyGrid">
  //data from the model(server side) filelist is not updating</div>

What will be the form onsubmit Javascript function to update the div tag with the data from the model.

My uploadfile controller get post methods are as:

[AcceptVerbs(HttpVerbs.Get)]
          public ActionResult Upload()
        {
            return View();
        }
 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddFile(HttpPostedFileBase addedFile)
        {
           static List<string> fileList = new List<string>();
        开发者_开发技巧   string filename = Path.GetFileName(addedFile.FileName);
            file.SaveAs(@"D:\Upload\" + filename);
            fileList.Add(filename);
            return("Upload",fileList);
}


In your post action method you are instantiating new list (fileList) every time a file is uploaded and no matter how many files you upload this list will contain only one entry in the current setup. i would suggest saving file list to database and retrieve it from there when you want to show the list on view. it could be like

[AcceptVerbs(HttpVerbs.Get)]
          public ActionResult Upload()
        {
            List<string> fileList = //retreive from db;  
            return View(fileList);
        }
 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddFile(HttpPostedFileBase addedFile)
        {

           string filename = Path.GetFileName(addedFile.FileName);
            file.SaveAs(@"D:\Upload\" + filename);
            //add file name in database
            return redirectToAction("Upload");
}

you should ideally always redirect from your post action method instead of returning a view if there are no model errors. In Get Action method you can retrieve the values from db and display it on the view. you can also put some parameter like id in both of your methods to save and retrieve values in database

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜