3
I’m a beginner in Asp.net MVC
I have a project.
And within a certain view I have two buttons.
However, one can only appear if the other is executed.
1 Button > Save (save car)
2 Button> Associate a dealership (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. That already works perfectly.
Can you help me? If you need more information, just say the word.
I believe I need a Cód javascript in the view, but I have no idea how to assemble it. I don’t 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 Carro
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();
}