Display a button based on the action of another

3

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();
        }
    
asked by anonymous 17.10.2015 / 22:38

1 answer

1

I think the best way would be to change the flow of your system so that the Associate button would only be shown when the user was editing a car.

For example, create an Action call Add, in this action you return a View called AddOrEdit.cshtml (reuse the same View).

public ActionResult Adicionar()
{
    return View("AdicionarOuEditar");
}

[HttpPost]
public ActionResult Adicionar(SuaModelAqui model)
{
    //seu código para salvar aqui
    return RedirectToAction("Editar", new { id = seuNovoIdAqui });
}

Create the corresponding Action for your HttpPost.

In your View, create a condition to show the button, something like:

@if(Model != null && Model.Id != 0)
{
   significa que sua view está em modo de edição
   mostra o botão
}

Then you create another action called Edit and follow the same steps as above, but with specific editing codes.

public ActionResult Editar(int id)
{
    var model = carregarSuaModelAqui(id);
    return View("AdicionarOuEditar", model);
}

[HttpPost]
public ActionResult Editar(SuaModelAqui model)
{
    //seu código para salvar aqui
    return Redirect(Request.RawUrl);
}
    
19.10.2015 / 21:26