i'M HAVING A TROUBLE WITH AN PROJECT, AN EXCEPTIONS IS RETURNED TO ME, I tried editing the model by trying to remove the nullable = true constraint, but the porblem persists
org.hibernate.PropertyValueException: not-null property references a null or transient
value
nested exception is org.hibernate.PropertyValueException: not-null property references
a null or transient value
org.springframework.dao.DataIntegrityViolationException: not-null property references a
null or transient value :
I attach below the structure of my project
Controller
#Controller
public class TemperatureController {
#Autowired
private TemperatureService temperatureService;
#RequestMapping(value="/", method= RequestMethod.GET) // inserisco il controllo per la
chiamata alla pagina principale
public ModelAndView homePage() { // creo un modelandview
ModelAndView mv = new ModelAndView(); // creo un'istanza del modelandView
mv.setViewName("home"); // setto il nome dell'istanza
List<InfoMeteo> listaIndicatoriMeteo = temperatureService.getAll();
mv.addObject("listaIndicatoriMeteo",listaIndicatoriMeteo);
mv.addObject("indicatoreMeteo", new InfoMeteo()); // creo la lista
return mv; // ritorno l'istanza
}
#RequestMapping(value="/", method= RequestMethod.POST)
public ModelAndView saveIndicatoriMeteo(InfoMeteo indicatoreMeteo) {
temperatureService.saveIndicatoriMeteo(indicatoreMeteo);
return new ModelAndView("redirect:/");
}
}
Model
#Entity
#Table(name="gestioneindicatorimeteo")
public class InfoMeteo implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO) // uso l'autoincrement
private long id;
#Column(name="Data")
private Date Data;
#Column(name="TemperaturaMax", nullable= false)
private byte TemperaturaGiornalieraMax;
#Column(name="TemperaturaMin", nullable= false)
private byte TemperaturaGiornalieraMin;
#Column(name="precipitazioni")
private String precipitazioni;
#Column(name="Quantità", nullable= false)
private byte quantitaprec;
#Column(name="Tempo", nullable= false)
private String tempo;
#Column(name="URMax", nullable= false)
private byte umiditaMax;
#Column(name="URMin", nullable= false)
private byte umiditaMin;
#Column(name="WSPDMax", nullable= false)
private short velocitaVentoMax;
#Column(name="WSPDMin", nullable= false)
private short velocitaVentoMin;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Date getData() {
return Data;
}
public void setData(Date data) {
Data = data;
}
public byte getTemperaturaGiornalieraMax() {
return TemperaturaGiornalieraMax;
}
public void setTemperaturaGiornalieraMax(byte temperaturaGiornalieraMax) {
TemperaturaGiornalieraMax = temperaturaGiornalieraMax;
}
public byte getTemperaturaGiornalieraMin() {
return TemperaturaGiornalieraMin;
}
public void setTemperaturaGiornalieraMin(byte temperaturaGiornalieraMin) {
TemperaturaGiornalieraMin = temperaturaGiornalieraMin;
}
public String getPrecipitazioni() {
return precipitazioni;
}
public void setPrecipitazioni(String precipitazioni) {
this.precipitazioni = precipitazioni;
}
public byte getQuantitaprec() {
return quantitaprec;
}
public void setQuantitaprec(byte quantitaprec) {
this.quantitaprec = quantitaprec;
}
public String getTempo() {
return tempo;
}
public void setTempo(String tempo) {
this.tempo = tempo;
}
public byte getUmiditaMax() {
return umiditaMax;
}
public void setUmiditaMax(byte umiditaMax) {
this.umiditaMax = umiditaMax;
}
public byte getUmiditaMin() {
return umiditaMin;
}
public void setUmiditaMin(byte umiditaMin) {
this.umiditaMin = umiditaMin;
}
public short getVelocitaVentoMax() {
return velocitaVentoMax;
}
public void setVelocitaVentoMax(short velocitaVentoMax) {
this.velocitaVentoMax = velocitaVentoMax;
}
public short getVelocitaVentoMin() {
return velocitaVentoMin;
}
public void setVelocitaVentoMin(short velocitaVentoMin) {
this.velocitaVentoMin = velocitaVentoMin;
}
}
Repository
#Repository("TemperatureRepository")
public interface TemperatureRepository extends JpaRepository<InfoMeteo, Long> {
}
Service
public interface TemperatureService {
void saveIndicatoriMeteo(InfoMeteo indicatoreMeteo);
List<InfoMeteo> getAll();
}
Service implementation
#Service("TemperatureService")
public class TemperatureServiceImpl implements TemperatureService {
#Autowired
private TemperatureRepository temperatureRepository; // inietto per dipendenza il
service del repository
#Override
public void saveIndicatoriMeteo(InfoMeteo indicatoreMeteo) {
temperatureRepository.save(indicatoreMeteo);
}
#Override
public List<InfoMeteo> getAll() {
return temperatureRepository.findAll();
}
}
Home.html
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/js/bootstrap.bundle.min.js">
</head>
<body>
<div class="container">
<div class="pb-3 mt-4 mb-2">
<div class = "btn btn-primary" >
</div>
<h2>Gestione Meteo - <small>Lista Indicatori Meteo</small></h2>
</div>
</div>
<div class="col-md-5" style="margin-top:50px;margin-bottom:50px;">
<form method="post" th:action="#{/}" th:object = "${indicatoreMeteo}">
<div class="form-group">
<label for= "data">Data</label>
<input type="text" " field="*{Data}" id="data>
class="form-control" required>
</div>
<div class="form-group">
<label for= "TemperaturaMax">Temperatura Max</label>
<input type="text" field="*{TemperaturaGiornalieraMax}" id="TemperaturaMax"
class="form-control" required>
</div>
<div class="form-group">
<label for= "TemperaturaMin">Temperatura Min</label>
<input type="text" id="TemperaturaMin"
field="*{TemperaturaGiornalieraMin}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "precipitazioni">Precipitazioni (prp)</label>
<input type="text" id="precipitazioni"
field="*{precipitazioni}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "Quantità">Quantità precipitazioni</label>
<input type="text" id="Quantità"
field="*{quantitaprec}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "Tempo">Condizioni meteo</label>
<input type="text" id="Tempo"
field="*{tempo}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "URMax">Ur max (Umidità)</label>
<input type="text" id="URMax"
field="*{umiditaMax}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "URMin">Ur Min (Umidità)</label>
<input type="text" id="URMin"
field="*{umiditaMin}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "WSPDMax">Velocità vento Min (Wv)</label>
<input type="text" id="WSPDMax"
field="*{velocitaVentoMax}"
class="form-control" required>
</div>
<div class="form-group">
<label for= "WSPDMin">Velocità vento Max(Wv)</label>
<input type="text" id="WSPDMin"
field="*{velocitaVentoMin}"
class="form-control" required>
</div>
<button style="margin-top:50px;" type="submit"
class="btn btn-dark"> Salva indicatori Meteo</button>
</form>
</div>
<table class="table table-striped">
<div table= "pb-2 mt-4 mb-3">
<thead>
<tr>
<th scope="col">Data</th>
<th scope="col">Temperatura Max giornaliera</th>
<th scope="col">Temperatura Min giornaliera</th>
<th scope="col">precipitazioni</th>
<th scope="col">Tipo di precipitazioni</th>
<th scope="col">Quantita prec </th>
<th scope="col">Condizioni meteo</th>
<th scope="col">Umidita Max</th>
<th scope="col">Umidita Min</th>
<th scope="col">Velocita Vento Max</th>
<th scope="col">Velocita Vento Min</th>
</tr>
<tbody>
<tr th:each="indicatoreMeteo: ${listaIndicatoriMeteo}">
<td th:text= "${indicatoreMeteo.Data}"></td>
<td th:text= "${indicatoreMeteo.TemperaturaGiornalieraMax}"></td>
<td th:text= "${indicatoreMeteo.TemperaturaGiornalieraMin}"></td>
<td th:text= "${indicatoreMeteo.precipitazioni}"></td>
<td th:text= "${indicatoreMeteo.quantitaprec}"></td>
<td th:text= "${indicatoreMeteo.tempo}"></td>
<td th:text= "${indicatoreMeteo.umiditaMax}"></td>
<td th:text= "${indicatoreMeteo.umiditaMin}"></td>
<td th:text= "${indicatoreMeteo.velocitaVentoMax}"></td>
<td th:text= "${indicatoreMeteo.velocitaVentoMin}"></td>
</tbody>
</table>
I have tried different solutions but it keeps giving me this kind of exception all the time, I don't know what to do
Related
I use two model in a single view so for that i make a class in i which I put both model.But when i use it, it gives me error.
Model:
namespace Laboratory_Management.Models
{
public class PatientandTest
{
public PatientTbl PatientTbl { get; set; }
public PatientTestTbl PatientTestTbl { get; set; }
}
}
View:
#model Laboratory_Management.Models.PatientandTest
#{
ViewData["Title"] = "Index1";
}
<h1>Index1</h1>
<h4>PatientTbl</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Index1" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="PatientTbl.PName" class="control-label"></label>
<input asp-for="PatientTbl.PName" class="form-control" id="PName" />
<span asp-validation-for="PatientTbl.PName" class="text-danger"></span>
</div>
</form>
</div>
</div>
Im trying to create a textbox component to recicle code, but i cant get the value from the textbox in a page. I only need to get the TextBox value from the component to use it into other pages.
this is the TextBox component :
<style>
.textbox-parameters{
background: Red;
}
</style>
#if (!string.IsNullOrWhiteSpace(Label))
{
<label for="#Id">#Label</label>
}
<div class="input-group mb-3 " >
<input type="text" class="form-control textbox-parameters" id="#Id" #bind="#CurrentValue">
</div>
#code {
[Parameter] public string? Label { get; set; }
[Parameter] public string? Id { get; set; }
private string CurrentValue {get;set;} = "";
}
and im trying to use there:
<div class="container-fluid">
<div class="row">
<div class="col-sm">
<label >Proyecto:</label>
<div class="input-group mb-3">
<input type="text" class="form-control" id="basic-url" aria-describedby="basic-addon3">
</div>
</div>
<div class="col-sm">
<p class="rcorners2">Lista modelos</p>
</div>
<div class="col-sm">
<p class="rcorners2">Lista Iteraciónes</p>
</div>
<div class="col-sm">
<p class="rcorners2">Imagen Semilla</p>
</div>
<div class="col-sm ">
<div class="row">
<div class="col-sm">
<label>Ancho</label>
<div class="input-group mb-3">
<div class="input-group-prepend"></div>
<input type="text" class="form-control textbox-parameters" id="basic-url" aria-describedby="basic-addon3">
</div>
</div>
<div class="col-sm">
<TextBox Id="Alto" Label="Alto" CurrentValue="#alto" />
</div>
<label>Texto: #alto</label>
</div>
<div class="row">
<div class="col-sm text-center">
<label>Numero Imágenes</label>
<div class="input-group mb-3 " >
<input type="text" class="form-control textbox-parameters" id="basic-url" aria-describedby="basic-addon3">
</div>
</div>
</div>
</div>
<div class="col-sm">
<button type="button" class="btn btn-outline-success">Generar</button>
</div>
</div>
</div>
#code{
string alto="";
}
Sorry for my extra text but StackOverflow need more details but thats my only problem with my code. thanks.
You need to set up binding correctly - i.e. set up the Value and ValueChanged parameters on the component.
Here's TextBox:
<style>
.textbox-parameters{
background: Red;
}
</style>
#if (!string.IsNullOrWhiteSpace(Label))
{
<label for="#Id">#Label</label>
}
<div class="input-group mb-3 " >
<input type="text" class="form-control textbox-parameters" id="#Id" value="#Value" #onchange="this.OnChange">
</div>
#code {
[Parameter] public string Value {get;set;} = "";
[Parameter] public EventCallback<string> ValueChanged {get;set;}
[Parameter] public string? Label { get; set; }
[Parameter] public string? Id { get; set; }
private void OnChange(ChangeEventArgs e)
{
ValueChanged.InvokeAsync(e.Value.ToString());
}
}
Note we've bound the value to Value and the change event to OnChange which then invokes ValueChanged.
And a test page:
#page "/"
<TextBox #bind-Value="#this.Value" />
<div>
Value = #this.Value
</div>
#code {
private string Value { get; set; } = "Fred";
}
#bind-Value tells the Razor pre-compiler to bind to Value to set the value and ValueChanged to update the bound field/property.
Anyone can help me? I made a Modal with a form to update a database. I receive data from Mysql DB but when i click on submit, the modal closes, but new parameters don't pass to the Spring controller... Where is the error?
Model class:
#Entity
#Table(name = "generi")
public class Genere {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int idGenere;
#Column
private String nomeGenere;
public Genere () {}
/**
* #param idGenere
* #param nomeGenere
*/
public Genere(int idGenere, String nomeGenere) {
setIdGenere(idGenere);
setNomeGenere(nomeGenere);
}
...and GETTERS and SETTERS
Service Class
#Service
public class GenereService {
#Autowired
private GenereRepository genereRepository;
public List<Genere> getGeneri(){
List<Genere> listGeneri = new ArrayList<Genere>();
genereRepository.findAll().forEach(listGeneri::add);
return listGeneri;
}
public void inserisciNuovoGenere(Genere genere) {
genereRepository.save(genere);
}
public void cancellaGenere(int idGenere) {
if (genereRepository.findById(idGenere) != null) {
genereRepository.deleteById(idGenere);
}
}
public void updateGenere (Genere genere) {
if (genereRepository.findById(genere.getIdGenere()) != null )
genereRepository.save(genere);
}
Controller
#Controller
public class GenereController {
#Autowired
private GenereService genereService;
#GetMapping("/generi")
public String getGeneri (Model model) {
model.addAttribute("newGen", new Genere());
model.addAttribute("generi", genereService.getGeneri());
return "generi";
}
#PostMapping("/generi")
public String postGeneriInsert(#ModelAttribute (name = "newGen") Genere newGen) {
genereService.inserisciNuovoGenere(newGen);
return "redirect:/generi";
}
#GetMapping("/generi/delete/{idGenere}")
public String getGenereDelete (#PathVariable int idGenere) {
genereService.cancellaGenere(idGenere);
return "redirect:/generi";
}
#PostMapping("/updateGen")
public String postUpdateGenere(#ModelAttribute Genere genere) {
genereService.updateGenere(genere);
return "redirect:/generi";
}
generi.html
<body>
<th:block th:include="fragments/update_scripts.html"></th:block>
<div class="container">
<div th:replace="fragments/common :: Logout"></div>
</div> <!-- end container -->
<div class="container login">
<h3>Gestione Generi</h3>
<form th:object="${newGen}" th:action="#{/generi}" method="post">
<div class="form-group">
<label for="gen">Aggiungi un nuovo genere</label>
<input type="text" id="gen" class="form-control" th:field="*{nomeGenere}" required="required">
</div>
<input type="submit" class="btn btn-primary" value="Aggiungi">
</form>
<br><br>
<table class="table table-stripped">
<thead>
<tr>
<th scope="col">Id</th>
<th scope="col">Genere</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="genere : ${generi}">
<td th:text="${genere.idGenere}"></td>
<td th:text="${genere.nomeGenere}"></td>
<td>
<button type="button"
class="btn btn-info btn-sm"
data-toggle="modal"
data-target="#updateGen"
th:attr="data-id=${genere.idGenere},
data-lab=${genere.nomeGenere}">Modifica</button>
<button type="button"
class="btn btn-outline-danger btn-sm"
data-toggle="modal"
data-target="#deleteConfirm"
th:attr="data-id=${genere.idGenere},
data-lab=${genere.nomeGenere}">Elimina</button>
</td>
</tr>
</tbody>
</table>
</div>
<div th:replace="fragments/common :: deleteConfirm"></div>
<div th:replace="fragments/update_modals :: updateGen"></div>
<div th:replace="fragments/common :: bootstrap"></div>
</body>
update_modal.html
<!-- modal Update Genere -->
<div class="modal fade" id="updateGen" tabindex="-1" th:fragment="updateGen" aria-labelledby="updGenModal">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="updGenModal">Modifica il Genere</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div>
<p id="Prova">Modal di Prova: </p>
</div>
<form th:action="#{/updateGen}" th:object="${genere}" method="post">
<input type="hidden" class="form-control" id="idGen" name="idGenere" value=""/>
<div class="form-group">
<label for="modGen" class="col-form-label">Genere</label>
<input type="text" class="form-control" id="modGen" name="nomeGenere" th:field="*{nomeGenere}" value="" required="required" />
</div>
</form>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-outline-warning" id="btnUpdGen" value="Aggiorna" data-dismiss="modal" />
<button type="button" class="btn btn-outline-secondary" data-dismiss="modal" >Annulla</button>
</div>
</div>
</div>
</div>
<!-- End modal Update Genere -->
Update_script
<script >
$(document).ready(function() {
$('#updateGen').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var id = button.data('id');
var label = button.data('lab');
var modal = $(this);
$('#Prova').html('Modifica -> ' + label);
modal.find('.modal-body #modGen').val(label);
})
});
</script>
And that's all...
Sorry for my long post, and if I made some errors creating it... but it's my first time on StackOverflow...
I'm trying to upload a image as a string field in an advert,but when adding a file to the body i got this error: "exception": "org.springframework.web.multipart.support.MissingServletRequestPartException", "message": "Required request part 'file' is not present".
I looked here for answers on this problem, but nothing helped me. I will be glad of any help.
My controller:
#Controller
#RequestMapping("/adverts")
public class AdvertController {
private AdvertService advertService;
private FileUploadService fileUploadService;
public AdvertController(AdvertService advertService, FileUploadService fileUploadService) {
this.advertService = advertService;
this.fileUploadService = fileUploadService;
}
#GetMapping("/showFormForAdd")
public String showFormForAdd(MultipartFile file, Model theModel) throws IOException {
Advert theAdvert = new Advert();
theModel.addAttribute("advert", theAdvert);
return "adverts/advert-form";
}
#PostMapping("/save")
public String saveAdvert(#RequestParam("file") MultipartFile file,
#AuthenticationPrincipal Account user,
#Valid #ModelAttribute("advert") Advert theAdvert,
BindingResult bindingResult) throws IOException {
if (bindingResult.hasErrors()) {
return "adverts/advert-form";
} else {
String filepath = fileUploadService.upload(file);
theAdvert.setFilename(filepath);
advertService.save(user, theAdvert);
}
return "redirect:/adverts/list";
}
}
My service:
#Service
public class FileUploadServiceImpl implements FileUploadService {
private String UPLOADED_FOLDER = "/images/";
#Override
public String upload(MultipartFile file) {
System.out.println(file);
if(file.isEmpty())
return null;
String fileName = null;
try {
fileName = generateFileName(Objects.requireNonNull(file.getOriginalFilename()));
byte[]bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + fileName);
Files.write(path, bytes);
} catch (IOException e) {
e.printStackTrace();
}
return "/resources/" + fileName;
}
private String generateFileName(String file){
String ext = file.substring(file.lastIndexOf("."));
return System.currentTimeMillis() + ext;
}
}
My html form for input:
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h3>Объявления о продаже недвижимости</h3>
<hr>
<p class="h4 mb-4">Добавить новое объявление в систему</p>
<form action="#" th:action="#{/adverts/save}"
th:object="${advert}" method="POST" enctype="multipart/form-data">
<!-- Add hidden form field to handle update -->
<input type="hidden" th:field="*{id}" />
<input type="text" th:field="*{title}"
class="form-control mb-4 col-4" placeholder="Название"/>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" class="alert alert-danger col-4" >Title Error</p>
<input type="file" name="file_upload"
class="form-control mb-4 col-4" placeholder="Изображение">
<!-- <input type="file" name="file"/>-->
<!-- <input type="hidden" class="form-control" th:field="*{photo}" placeholder="Enter Image"/>-->
<input type="text" th:field="*{price}"
class="form-control mb-4 col-4" placeholder="Цена">
<p th:if="${#fields.hasErrors('price')}" th:errors="*{price}" class="alert alert-danger col-4" >Price Error</p>
<input type="text" th:field="*{description}"
class="form-control mb-4 col-4" placeholder="Описание">
<p th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="alert alert-danger col-4" >Description Error</p>
<button type="submit" class="btn btn-info col-2">Добавить</button>
</form>
<a th:href="#{/adverts/list}">Назад к списку объявлений</a>
</div>
</body>
</html>
Change
<input type="file" name="file_upload" class="form-control mb-4 col-4" placeholder="Изображение">
To
<input type="file" name="file" class="form-control mb-4 col-4" placeholder="Изображение">
Your controller expects a param file, but from html you are sending file_upload. Thats why spring shows error message "Required request part 'file' is not present"
I want to have News can have multiple NewsCategories. one-to-many relationship. I am trying to pass categories to MVC controller (post create action). But ModelState.IsValid is always failed. I think my post action does not get selected values from view somehow..
Please help my friend geeks!
I have News.cs:
public class News
{
public long NewsId { get; set; }
public ICollection<NewsCategory> NewsCategories { get; set; }
}
I have NewsCategory.cs:
public class NewsCategory
{
public int NewsCategoryId { get; set; }
public long NewsId { get; set; }
public string Title { get; set; }
}
I have NewsController.cs:
// GET: News/Create
public IActionResult Create()
{
List<NewsCategory> newsCategories = new List<NewsCategory>();
newsCategories = (from s in _context.NewsCategory select s).OrderBy(m => m.Title).ToList();
newsCategories.Insert(0, new NewsCategory { NewsCategoryId = 0, Title = "Select" });
ViewBag.NewsCategories = newsCategories;
return View();
}
And this is Create Post action:
// POST: News/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Title,Content,NewsTagName1,NewsTagName2")]News news, List<IFormFile> files)
{
if (ModelState.IsValid)
{
_context.Add(news);
var newsCategories = news.NewsCategories;
foreach(var item in newsCategories)
{
item.NewsId = news.NewsId;
_context.NewsCategory.Add(item);
}
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(news);
}
But always fails on 'Model.IsValid'
Here is snapshot of what's in object News.
Here is the error message:
Here is News create view:
#model Carsubfax.Models.News
#{
ViewData["Title"] = "Create";
}
<form asp-action="Create" id="comment-form" name="comment-form" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" id="txtTitle" autocomplete="off" required="required" placeholder="Enter your news title." />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
</div>
<div class="col-md-10 col-md-offset-1">
<div class="form-group">
<label class="control-label">News Cover Image<span style="color:red">*</span></label>
<input type="file" name="files" required />
</div>
</div>
<div class="col-md-10 col-md-offset-1">
<div class="form-group">
<label class="control-label">News Category</label>
<select asp-for="NewsCategories" class="form-control" asp-items="#(new SelectList(ViewBag.NewsCategories, "NewsCategoryId", "Title"))" required="required"></select>
</div>
<div class="col-md-10 col-md-offset-1">
<div class="form-group text-center">
<button type="submit" class="btn btn-primary" onclick="return btnSubmit_clik();">POST</button>
</div>
</div>
<div class="col-md-10 col-md-offset-1">
<div class="form-group text-center">
<a asp-action="Index">Back to List</a>
</div>
</div>
</div>
</form>
</div>
</div>