I'm a beginner in ASP.NET MVC
I have a project.
And within a given view I have two buttons.
However, one of them can only appear if the other is executed.
1 > Save (save car)
2 > Associate a dealership (it can only be activated when I save the car)
Associating a dealership is a PopUp that displays a grid with checkbox and allows the user to select a dealership to associate with. This already works perfectly.
Can you help me? If you need more information, just talk.
I believe I need to have a JavaScript code in the view, but I have no idea how to mount it. I do not have enough knowledge in js.
Both are in the same view.
View
@model Carro
@{
var idConcessionaria = "id" + Guid.NewGuid().ToString().Substring(0, 5);
}
<div class="box-fields">
@using (Ajax.BeginForm(
"Incluir",
"Carro",
new DefaultAjaxOptions()
))
{
@Html.Partial("Validation")
@Html.HiddenFor(i => i.Id)
@Html.EditorFor(i => i.Modelo)
@Html.EditorFor(i => i.Codigo)
[...]
<div class="clear">
<div class="box-button">
<input type="submit" class="button blue" value="@Geral.Salvar" />
</div>
<div id="@idConcessionaria" class="box-button">
<a class="button blue" href="#@idConcessionaria" onclick="openPopUpConcessionaria()" >@AdicionarConcessionaria</a>
</div>
</div>
}
<script language="javascript" type="text/javascript">
function openPopUpConcessionaria() {
var newGuid = guid();
$('#console').load('@Url.Action("SelecionarConcessionaria", "Concessionaria")');
return false;
}
</script>
Controller Car
public ViewResultBase Salvar()
{
Carro model = new Carro();
return base.SwitchView(model);
}
[HttpPost]
[OutputCache(Duration = 0)]
public ViewResultBase Salvar([ModelBinder(typeof(CollectionModelBinder))]Carro model)
{
if (this.ModelState.IsValid)
{
try
{
this.Service.AddItem(model);
return this.PopUpSuccessView();
}
catch (ValidationException exception)
{
base.AddValidationErrors(exception);
return base.PartialView(model);
}
}
else
{
return base.PartialView(model);
}
}
Services
public void AddItem(Carro item)
{
base.context.Carros.Add(item);
base.Save();
}