add news and stuff
This commit is contained in:
@ -19,6 +19,9 @@ namespace WebApplication1.Controllers
|
||||
// GET: news
|
||||
public ActionResult Index()
|
||||
{
|
||||
var news = db.news.ToList();
|
||||
var users = db.users.ToList();
|
||||
var models = new Tuple<List<news>, List<user>>(news, users);
|
||||
return View(db.news.ToList());
|
||||
}
|
||||
|
||||
@ -34,6 +37,8 @@ namespace WebApplication1.Controllers
|
||||
{
|
||||
return HttpNotFound();
|
||||
}
|
||||
var user = db.users.Find(news.userID);
|
||||
// var model = new Tuple<object,object>(news,user);
|
||||
return View(news);
|
||||
}
|
||||
|
||||
@ -67,46 +72,59 @@ namespace WebApplication1.Controllers
|
||||
if (Request.Files.Count > 0)
|
||||
{
|
||||
setLists();
|
||||
var image = Request.Files[0];
|
||||
if ( image.ContentLength > 1024 * 1024)
|
||||
|
||||
if (news.title != null && news.link != null && news.cat != null && news.tag != null)
|
||||
{
|
||||
ModelState.AddModelError("imageFile", "File size cannot exceed 1MB.");
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
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.");
|
||||
// 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.");
|
||||
}
|
||||
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;
|
||||
}
|
||||
news.userID = Convert.ToInt32(Session["Userid"]);
|
||||
news.views = 0;
|
||||
news.publishDate = DateTime.Now;
|
||||
|
||||
|
||||
db.news.Add(news);
|
||||
db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
|
||||
}
|
||||
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];
|
||||
}
|
||||
|
||||
image.SaveAs(Server.MapPath($"~/Media/news/"+imgname+fileExtension));
|
||||
ModelState.AddModelError("", "no field can be empty");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/*
|
||||
Request.Files[i].SaveAs(Server.MapPath($"~/Media/User/avatar.png"));
|
||||
|
||||
db.news.Add(news);
|
||||
db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
*/
|
||||
}
|
||||
|
||||
return View(news);
|
||||
@ -170,7 +188,6 @@ namespace WebApplication1.Controllers
|
||||
}
|
||||
void setLists()
|
||||
{
|
||||
var news = db.news.ToList();
|
||||
var cat = db.categories.ToList();
|
||||
ViewBag.categoryTitle = cat;
|
||||
var tags = db.tags.ToList();
|
||||
|
||||
|
Before Width: | Height: | Size: 297 KiB After Width: | Height: | Size: 297 KiB |
@ -54,7 +54,7 @@
|
||||
<div class="form-group">
|
||||
@Html.LabelFor(model => model.tag, htmlAttributes: new { @class = "control-label col-md-2" })
|
||||
<div class="col-md-10">
|
||||
<select id="cat" name="cat" class="form-control" multiple>
|
||||
<select id="tag" name="tag" class="form-control" multiple>
|
||||
@foreach (var item in ViewBag.tagTitle)
|
||||
{
|
||||
<option value="@item.title">@item.title</option>
|
||||
|
||||
@ -9,7 +9,7 @@
|
||||
<div>
|
||||
<h4>news</h4>
|
||||
<hr />
|
||||
<dl class="dl-horizontal">
|
||||
<dl>
|
||||
<dt>
|
||||
@Html.DisplayNameFor(model => model.title)
|
||||
</dt>
|
||||
@ -23,6 +23,7 @@
|
||||
</dt>
|
||||
|
||||
<dd>
|
||||
<img src="@Model.image" />
|
||||
@Html.DisplayFor(model => model.image)
|
||||
</dd>
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
@model IEnumerable<WebApplication1.Models.news>
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Index";
|
||||
}
|
||||
@ -14,30 +13,15 @@
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.title)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.image)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.link)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.summary)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.cat)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.tag)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.publishDate)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.userID)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.Content)
|
||||
</th>
|
||||
<th>
|
||||
@Html.DisplayNameFor(model => model.views)
|
||||
</th>
|
||||
@ -49,30 +33,15 @@
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.title)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.image)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.link)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.summary)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.cat)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.tag)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.publishDate)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.userID)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.Content)
|
||||
</td>
|
||||
<td>
|
||||
@Html.DisplayFor(modelItem => item.views)
|
||||
</td>
|
||||
|
||||
Reference in New Issue
Block a user