fix edit news
This commit is contained in:
@ -64,6 +64,9 @@ namespace WebApplication1.Controllers
|
|||||||
// GET: news/Details/5
|
// GET: news/Details/5
|
||||||
public ActionResult Details(int? id)
|
public ActionResult Details(int? id)
|
||||||
{
|
{
|
||||||
|
string userrole = Convert.ToString(Session["Userrole"]);
|
||||||
|
ViewBag.role = userrole.Contains("tag&catManagment") ? 1 : 0;
|
||||||
|
|
||||||
if (id == null)
|
if (id == null)
|
||||||
{
|
{
|
||||||
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
||||||
@ -112,7 +115,7 @@ namespace WebApplication1.Controllers
|
|||||||
|
|
||||||
if (userrole.Contains("addNews"))
|
if (userrole.Contains("addNews"))
|
||||||
{
|
{
|
||||||
ViewBag.categoryTitle = db.categories.ToList();
|
setLists();
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -176,7 +179,9 @@ namespace WebApplication1.Controllers
|
|||||||
if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension))
|
if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension))
|
||||||
{
|
{
|
||||||
ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed.");
|
ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed.");
|
||||||
|
news.image = "";
|
||||||
return View();
|
return View();
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -224,8 +229,8 @@ namespace WebApplication1.Controllers
|
|||||||
if (Session["User"] != null)
|
if (Session["User"] != null)
|
||||||
{
|
{
|
||||||
string userrole = Convert.ToString(Session["Userrole"]);
|
string userrole = Convert.ToString(Session["Userrole"]);
|
||||||
|
setLists();
|
||||||
|
|
||||||
if (id == null)
|
if (id == null)
|
||||||
{
|
{
|
||||||
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
||||||
@ -253,11 +258,82 @@ namespace WebApplication1.Controllers
|
|||||||
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[ValidateAntiForgeryToken]
|
[ValidateAntiForgeryToken]
|
||||||
public ActionResult Edit([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news)
|
public ActionResult Edit([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news, List<string> Tags, List<string> Cat)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
db.Entry(news).State = EntityState.Modified;
|
|
||||||
|
news.cat = "";
|
||||||
|
if (Tags != null)
|
||||||
|
{
|
||||||
|
news.tag = "";
|
||||||
|
foreach (string T in Tags)
|
||||||
|
{
|
||||||
|
if (T != Tags[0])
|
||||||
|
{
|
||||||
|
news.tag += " ,";
|
||||||
|
news.tag += T;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
news.tag += T;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Cat != null){
|
||||||
|
foreach (string c in Cat)
|
||||||
|
{
|
||||||
|
if (c != Cat[0])
|
||||||
|
{
|
||||||
|
news.cat += " ,";
|
||||||
|
news.cat += c;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
news.cat += c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var image = Request.Files[0];
|
||||||
|
if (image.ContentLength > 1024 * 1024)
|
||||||
|
{
|
||||||
|
//check if fields are empty
|
||||||
|
ModelState.AddModelError("imageFile", "File size cannot exceed 1MB.");
|
||||||
|
return RedirectToAction("Index");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check file extension
|
||||||
|
string fileExtension = Path.GetExtension(image.FileName).ToLower();
|
||||||
|
//if (fileExtension != ".jpg" || fileExtension != ".jpeg" || fileExtension != ".png")
|
||||||
|
if (!new[] { ".jpg", ".jpeg", ".png" }.Contains(fileExtension))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("image", "Only JPG, JPEG, and PNG files are allowed.");
|
||||||
|
news.image = "";
|
||||||
|
return View();
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int length = 10; // Desired length of the random string
|
||||||
|
string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||||
|
char[] chars = new char[length];
|
||||||
|
Random rand = new Random();
|
||||||
|
string imgname = "";
|
||||||
|
for (int i = 0; i < length; i++)
|
||||||
|
{
|
||||||
|
chars[i] = allowedChars[rand.Next(0, allowedChars.Length)];
|
||||||
|
imgname += "" + chars[i];
|
||||||
|
}
|
||||||
|
var path = $"~/Media/news/" + imgname + fileExtension;
|
||||||
|
image.SaveAs(Server.MapPath(path));
|
||||||
|
news.image = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
//db op
|
||||||
|
var curnews = db.news.Find(news.ID);
|
||||||
|
curnews.title = news.title != null ? news.title : curnews.title;
|
||||||
|
curnews.image = news.image != null ? news.image : curnews.image;
|
||||||
|
curnews.summary = news.summary != null ? news.summary : curnews.summary;
|
||||||
|
curnews.Content = news.Content != null ? news.Content : curnews.Content;
|
||||||
|
curnews.link = news.link != null ? news.link : curnews.link;
|
||||||
|
curnews.tag = news.tag != null ? news.tag : curnews.tag;
|
||||||
|
curnews.cat = news.cat != null ? news.cat : curnews.cat;
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
Media/news/kgeN7kkWJ0.jpg
Normal file
BIN
Media/news/kgeN7kkWJ0.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 297 KiB |
@ -94,6 +94,12 @@
|
|||||||
</dl>
|
</dl>
|
||||||
</div>
|
</div>
|
||||||
<p>
|
<p>
|
||||||
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
|
@if (@ViewBag.role == 1)
|
||||||
|
{
|
||||||
|
@Html.ActionLink("Edit", "Edit", new { id = Model.ID })
|
||||||
|
<span>
|
||||||
|
|
|
||||||
|
</span>
|
||||||
|
}
|
||||||
@Html.ActionLink("Back to List", "Index")
|
@Html.ActionLink("Back to List", "Index")
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@ -6,15 +6,14 @@
|
|||||||
|
|
||||||
<h2>Edit</h2>
|
<h2>Edit</h2>
|
||||||
|
|
||||||
@using (Html.BeginForm())
|
@using (Html.BeginForm("Edit", "news", FormMethod.Post, new { enctype = "multipart/form-data" }))
|
||||||
{
|
{
|
||||||
@Html.AntiForgeryToken()
|
@Html.AntiForgeryToken()
|
||||||
|
|
||||||
<div class="form-horizontal">
|
<div class="form">
|
||||||
<h4>news</h4>
|
<h4>news</h4>
|
||||||
<hr />
|
<hr />
|
||||||
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
|
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
|
||||||
@Html.HiddenFor(model => model.ID)
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.title, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
@ -24,14 +23,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
@Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
|
|
||||||
<div class="col-md-10">
|
|
||||||
@Html.EditorFor(model => model.image, new { htmlAttributes = new { @class = "form-control" } })
|
|
||||||
@Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.link, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
@ -41,17 +32,21 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.image, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
@Html.EditorFor(model => model.summary, new { htmlAttributes = new { @class = "form-control" } })
|
@Html.EditorFor(model => model.image, new { htmlAttributes = new { @type = "file", @class = "form-control" } })
|
||||||
@Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" })
|
@Html.ValidationMessageFor(model => model.image, "", new { @class = "text-danger" })
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.cat, htmlAttributes: new { @class = "control-label col-md-2" })
|
<label class="control-label col-md-2">category</label>
|
||||||
<div class="col-md-10">
|
<div>
|
||||||
@Html.EditorFor(model => model.cat, new { htmlAttributes = new { @class = "form-control" } })
|
<select id="cat" name="Cat" class="form-control" multiple>
|
||||||
|
@foreach (var item in ViewBag.categoryTitle)
|
||||||
|
{
|
||||||
|
<option value="@item.title">@item.title</option>
|
||||||
|
}
|
||||||
|
</select>
|
||||||
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
|
@Html.ValidationMessageFor(model => model.cat, "", new { @class = "text-danger" })
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -59,46 +54,35 @@
|
|||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
@Html.EditorFor(model => model.tag, new { htmlAttributes = new { @class = "form-control" } })
|
<select id="tag" name="Tags" class="form-control" multiple>
|
||||||
|
@foreach (var item in ViewBag.tagTitle)
|
||||||
|
{
|
||||||
|
<option value="@item.title">@item.title</option>
|
||||||
|
}
|
||||||
|
</select>
|
||||||
@Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" })
|
@Html.ValidationMessageFor(model => model.tag, "", new { @class = "text-danger" })
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.publishDate, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.summary, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
@Html.EditorFor(model => model.publishDate, new { htmlAttributes = new { @class = "form-control" } })
|
@Html.TextAreaFor(model => model.summary, new { @class = "form-control" })
|
||||||
@Html.ValidationMessageFor(model => model.publishDate, "", new { @class = "text-danger" })
|
@Html.ValidationMessageFor(model => model.summary, "", new { @class = "text-danger" })
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
|
||||||
@Html.LabelFor(model => model.userID, htmlAttributes: new { @class = "control-label col-md-2" })
|
|
||||||
<div class="col-md-10">
|
|
||||||
@Html.EditorFor(model => model.userID, new { htmlAttributes = new { @class = "form-control" } })
|
|
||||||
@Html.ValidationMessageFor(model => model.userID, "", new { @class = "text-danger" })
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
|
@Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||||
<div class="col-md-10">
|
<div class="col-md-10">
|
||||||
@Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
|
@Html.TextAreaFor(model => model.Content, new { @class = "form-control" })
|
||||||
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
|
@Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<br />
|
||||||
<div class="form-group">
|
|
||||||
@Html.LabelFor(model => model.views, htmlAttributes: new { @class = "control-label col-md-2" })
|
|
||||||
<div class="col-md-10">
|
|
||||||
@Html.EditorFor(model => model.views, new { htmlAttributes = new { @class = "form-control" } })
|
|
||||||
@Html.ValidationMessageFor(model => model.views, "", new { @class = "text-danger" })
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<div class="col-md-offset-2 col-md-10">
|
<div class="col-md-offset-2 col-md-10">
|
||||||
<input type="submit" value="Save" class="btn btn-default" />
|
<input type="submit" value="Create" class="btn btn-outline-dark" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -30,7 +30,6 @@
|
|||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@Html.ActionLink("Edit", "Edituser", new { id=item.ID }) |
|
@Html.ActionLink("Edit", "Edituser", new { id=item.ID }) |
|
||||||
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
|
|
||||||
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
|
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
Reference in New Issue
Block a user