For example, in my project I'm using @using(Html.BeginForm(some stuff))
to create an instance of my model and finally appending it to my database. But, when I'm editing (updating) some object of model, I'm usingn ajax get to retrieve data and finally using @Html.BeginForm() to post the update. Well, this is breaking some pattern of coding in MVC or whatever good principle?
Retrieving data and filling in the input fields
$.ajax({
url: "../../Obra/" + id,
type: "POST",
success: function(data) {
var obj = JSON.parse(JSON.stringify(data));
tit.value = obj.titulo;
ed.value = obj.editora;
obj.autores.forEach(x => {
var newn = divAutor.appendChild(aut.cloneNode());
newn.value = x;
});
img.value = obj.imagem;
divAutor.removeChild(aut);
},
error: function() {
alert('Error.');
}
});
All modal:
<div class="modal" id="modalOverlay">
<div class="modal-content" id="modalContent">
<header class="modal-header">
<h1 id="updateObraModalTitulo"></h1>
</header>
@using(@Html.BeginForm("Update", "Obra", FormMethod.Post, new { id="form-update" })) {
<div class="updateObraDivProp">
@Html.LabelFor(m => m.Titulo, "Título:")
@Html.TextBoxFor(m => m.Titulo, new { @class="editInputValue", @id="editTituloInput" })
</div>
<div class="updateObraDivProp">
@Html.LabelFor(m => m.Editora, "Editora:")
@Html.TextBoxFor(m => m.Editora, new { @class="editInputValue", @id="editEditoraInput"})
</div>
<div class="updateObraDivProp">
@Html.LabelFor(m => m.Autores, "Autores:")
<div id="divInputAutores">
@Html.TextBoxFor(m => m.Autores, new { @class="inputPostValue", id="addAutoresInput"})
</div>
<div class="btnNewAutorOp">
<button type="button" id="btnAddAutor" onclick="addNewAutor()">+</button>
<button type="button" id="btnDeleteAutor" onclick="deleteNewAutor()">×</button>
</div>
</div>
<div class="updateObraDivProp">
@Html.LabelFor(m => m.Imagem, "Imagem:")
@Html.TextBoxFor(m => m.Imagem, new { @class="inputPostValue", @id="editImagemInput" })
</div>
<input type="submit">
}
</div>
</div>
Because I can't retrieve the Id property when I click in the modal with mvc (or maybe I can using partial views?), I'm using javascript to create a function with the param id and getting data with ajax and finally updating with form