I am having some trouble. I have a Telerik editor where I display I file that is housed in my application. I need to save the changes back to the file. But some things also get saved to back to the database. My save button currently does save back to the database, but I don't know how to also make the file save the changes made. Here is my page code:
@model BbkValidation.Models.Task
@{
ViewBag.Title = "Edit";
}
<!--<h2>Edit</h2>-->
<h2>@Html.DisplayFor(model => model.Title)</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
@Html.HiddenFor(model => model.TaskID)
@Html.HiddenFor(model => model.Title)
@Html.HiddenFor(model => model.Url)
<div style="overflow: auto; width: 100%;">
<div class="editor-field;" style="margin-top: 5px; margin-bottom: 20px; float: left">
Task Status<br />
@Html.RadioButton("Status", "Open") Open<br />
@Html.RadioButton("Status", "Meditech") Meditech<br />
@Html.RadioButton("Status", "Closed") Closed<br />
@Html.ValidationMessageFor(model => model.Status)
</div>
<div style="float: right; margin-top: 5px; margin-bottom: 5px; margin-left: 17px">
Notes<br />
@Html.TextAreaFor(model => model.Notes)
@Html.ValidationMessageFor(model => model.Notes)
</div>
<div>
@{
string htmlFilePath = "~/TaskHtml/" + Model.Url;
string fileContents = "";
string physicalPath = Server.MapPath(htmlFilePath);
fileContents = File.ReadAllText(physicalPath);
Html.Telerik().Editor()
.Name("editor")
.HtmlAttributes(new { style = "height:500px; width:99%;frameborder:0" })
.Encode(false)
.Value(@fileContents).Render();
}
</div>
</div>
<p>
<input type="submit" value="Save" />
@Html.ActionLink("Cancel, back to list", "Index")
</p>
<!-- other content -->
</fieldset>
}
Here is my task controller code:
namespace BbkValidation.Controllers
{
public class TasksController : Controller
{
private TaskDBContext db = new TaskDBContext();
//
// GET: /Tasks/
/*************start original
public ViewResult Index()
{
return View(db.Tasks.ToList());
}
***********************************end orig*/
//[Authorize(Roles = @"IS Users")]
public ViewResult Index(string sortOrder, string searchString)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
ViewBag.StatusSortParm = sortOrder == "Status" ? "Status desc" : "Status";
var tasks = from t in db.Tasks
select t;
if (!String.IsNullOrEmpty(searchString))
{
tasks = tasks.Where(t => t.Title.Contains(searchString));
}
switch (sortOrder)
{
case "Name desc":
tasks = tasks.OrderByDescending(t => t.Title);
break;
case "Status desc":
tasks = tasks.OrderBy(t => t.Status);
break;
default:
tasks = tasks.OrderBy(t => t.TaskID);
break;
}
return View(tasks.ToList());
}
//
// GET: /Tasks/Details/5
//[Authorize(Roles = @"IS Users")]
public ViewResult Details(int id)
{
Task task = db.Tasks.Find(id);
return View(task);
}
//
// GET: /Tasks/Create
//[Authorize(Roles = @"IS Users")]
public ActionResult Create()
{
return View();
}
//
// POST: /Tasks/Create
[HttpPost]
//[Authorize(Roles = @"IS Users")]
public ActionResult Create(Task task)
{
if (ModelState.IsValid)
{
db.Tasks.Add(task);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(task);
}
//
// GET: /Tasks/Edit/5
//[Authorize(Roles = @"IS Users")]
public ActionResult Edit(int id)
{
Task task = db.Tasks.Find(id);
return View(task);
}
//
// POST: /Tasks/Edit/5
[HttpPost]
//[Authorize(Roles = @"IS Users")]
public ActionResult Edit(Task task)
{
if (ModelState.IsValid)
{
db.Entry(task).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(task);
}
//
// GET: /Tasks/Delete/5
//[Authorize(Roles = @"IS Users")]
public ActionResult Delete(int id)
{
Task task = db.Tasks.Find(id);
return View(task);
}
//
// POST: /Tasks/Delete/5
//[Authorize(Roles = @"IS Users")]
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Task task = db.Tasks.Find(id);
db.Tasks.Remove(task);
db.SaveChanges();
return RedirectToAction("Index");
}
//[Authorize(Roles = @"IS Users")]
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
//[HttpGet]
//public ActionResult ShowFile()
//{
// string filepath = Server.MapPath("~/~/TaskHtml/(model => model.Url)");
// string content = string.Empty;
// try
// {
// using (var stream = new StreamReader(filepath))
// {
// content = stream.ReadToEnd();
// }
// }
// catch (Exception exc)
// {
// return Content("Uh oh!");
// }
// return Content(content);
//}
}
}
And here is my model code:
namespace BbkValidation.Models
{
public class Task
{
public int TaskID { get; set; }
public string Title { get; set; }
public string Status { get; set; }
public string Url { get; set; }
public string Notes { get; set; }
}
public class TaskDBContext : DbContext
{
public DbSet<Task> Tasks { get; set; }
}
}
Please let mw know what I can do. Thank you.