add news and stuff

This commit is contained in:
2024-07-30 17:51:04 +03:30
parent 323910fb41
commit f310a94d5c
5 changed files with 51 additions and 64 deletions

View File

@ -19,6 +19,9 @@ namespace WebApplication1.Controllers
// GET: news // GET: news
public ActionResult Index() 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()); return View(db.news.ToList());
} }
@ -34,6 +37,8 @@ namespace WebApplication1.Controllers
{ {
return HttpNotFound(); return HttpNotFound();
} }
var user = db.users.Find(news.userID);
// var model = new Tuple<object,object>(news,user);
return View(news); return View(news);
} }
@ -67,9 +72,13 @@ namespace WebApplication1.Controllers
if (Request.Files.Count > 0) if (Request.Files.Count > 0)
{ {
setLists(); setLists();
var image = Request.Files[0];
if ( image.ContentLength > 1024 * 1024) if (news.title != null && news.link != null && news.cat != null && news.tag != null)
{ {
var image = Request.Files[0];
if (image.ContentLength > 1024 * 1024)
{
//check if fields are empty
ModelState.AddModelError("imageFile", "File size cannot exceed 1MB."); ModelState.AddModelError("imageFile", "File size cannot exceed 1MB.");
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
@ -87,26 +96,35 @@ namespace WebApplication1.Controllers
string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char[] chars = new char[length]; char[] chars = new char[length];
Random rand = new Random(); Random rand = new Random();
string imgname=""; string imgname = "";
for (int i = 0; i < length; i++) for (int i = 0; i < length; i++)
{ {
chars[i] = allowedChars[rand.Next(0, allowedChars.Length)]; chars[i] = allowedChars[rand.Next(0, allowedChars.Length)];
imgname = "" + chars[i]; imgname += "" + chars[i];
} }
var path = $"~/Media/news/" + imgname + fileExtension;
image.SaveAs(Server.MapPath($"~/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;
}
/*
Request.Files[i].SaveAs(Server.MapPath($"~/Media/User/avatar.png"));
db.news.Add(news); db.news.Add(news);
db.SaveChanges(); db.SaveChanges();
return RedirectToAction("Index"); return RedirectToAction("Index");
*/
}
else
{
ModelState.AddModelError("", "no field can be empty");
}
}
} }
return View(news); return View(news);
@ -170,7 +188,6 @@ namespace WebApplication1.Controllers
} }
void setLists() void setLists()
{ {
var news = db.news.ToList();
var cat = db.categories.ToList(); var cat = db.categories.ToList();
ViewBag.categoryTitle = cat; ViewBag.categoryTitle = cat;
var tags = db.tags.ToList(); var tags = db.tags.ToList();

View File

Before

Width:  |  Height:  |  Size: 297 KiB

After

Width:  |  Height:  |  Size: 297 KiB

View File

@ -54,7 +54,7 @@
<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">
<select id="cat" name="cat" class="form-control" multiple> <select id="tag" name="tag" class="form-control" multiple>
@foreach (var item in ViewBag.tagTitle) @foreach (var item in ViewBag.tagTitle)
{ {
<option value="@item.title">@item.title</option> <option value="@item.title">@item.title</option>

View File

@ -9,7 +9,7 @@
<div> <div>
<h4>news</h4> <h4>news</h4>
<hr /> <hr />
<dl class="dl-horizontal"> <dl>
<dt> <dt>
@Html.DisplayNameFor(model => model.title) @Html.DisplayNameFor(model => model.title)
</dt> </dt>
@ -23,6 +23,7 @@
</dt> </dt>
<dd> <dd>
<img src="@Model.image" />
@Html.DisplayFor(model => model.image) @Html.DisplayFor(model => model.image)
</dd> </dd>

View File

@ -1,5 +1,4 @@
@model IEnumerable<WebApplication1.Models.news> @model IEnumerable<WebApplication1.Models.news>
@{ @{
ViewBag.Title = "Index"; ViewBag.Title = "Index";
} }
@ -14,30 +13,15 @@
<th> <th>
@Html.DisplayNameFor(model => model.title) @Html.DisplayNameFor(model => model.title)
</th> </th>
<th>
@Html.DisplayNameFor(model => model.image)
</th>
<th>
@Html.DisplayNameFor(model => model.link)
</th>
<th>
@Html.DisplayNameFor(model => model.summary)
</th>
<th> <th>
@Html.DisplayNameFor(model => model.cat) @Html.DisplayNameFor(model => model.cat)
</th> </th>
<th>
@Html.DisplayNameFor(model => model.tag)
</th>
<th> <th>
@Html.DisplayNameFor(model => model.publishDate) @Html.DisplayNameFor(model => model.publishDate)
</th> </th>
<th> <th>
@Html.DisplayNameFor(model => model.userID) @Html.DisplayNameFor(model => model.userID)
</th> </th>
<th>
@Html.DisplayNameFor(model => model.Content)
</th>
<th> <th>
@Html.DisplayNameFor(model => model.views) @Html.DisplayNameFor(model => model.views)
</th> </th>
@ -49,30 +33,15 @@
<td> <td>
@Html.DisplayFor(modelItem => item.title) @Html.DisplayFor(modelItem => item.title)
</td> </td>
<td>
@Html.DisplayFor(modelItem => item.image)
</td>
<td>
@Html.DisplayFor(modelItem => item.link)
</td>
<td>
@Html.DisplayFor(modelItem => item.summary)
</td>
<td> <td>
@Html.DisplayFor(modelItem => item.cat) @Html.DisplayFor(modelItem => item.cat)
</td> </td>
<td>
@Html.DisplayFor(modelItem => item.tag)
</td>
<td> <td>
@Html.DisplayFor(modelItem => item.publishDate) @Html.DisplayFor(modelItem => item.publishDate)
</td> </td>
<td> <td>
@Html.DisplayFor(modelItem => item.userID) @Html.DisplayFor(modelItem => item.userID)
</td> </td>
<td>
@Html.DisplayFor(modelItem => item.Content)
</td>
<td> <td>
@Html.DisplayFor(modelItem => item.views) @Html.DisplayFor(modelItem => item.views)
</td> </td>