Display a button based on the action of another

Asked

Viewed 124 times

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();
        }

1 answer

1


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

For example, create an Action called Add, in this action you return a View called Add.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 your Httppost Action.

In your View, create a condition to show or not 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 previous steps, but with specific codes for editing.

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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.