How to pass model between different method of controller from view MVC

Jonathan Burgos Gio

I search much but I am not lucky to find the solution, My aim is save a model depending on the button that the user chooses.

I have two inputs of type button, which should each invoke a different method from the controller at the moment of press click. You must have an account. All this happens in the same view for only a model.

This is my view:

@model WebShop.Models.Product

@{
    ViewBag.Title = "Create";
}

<h2>Crear</h2>

@using Newtonsoft.Json  
@using (Html.BeginForm(new { @id = "create" })) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Producto</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.ProductNumber, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductNumber, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductNumber, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ProductTitle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ProductTitle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ProductTitle, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Crear" class="btn btn-default" />
                <input type="submit" value="Crear en memoria" class="btn btn-default" id="InMemory" />
            </div>
        </div>       
    </div>
}

And these are my methods:

[HttpPost]
public ActionResult Create(Product product)
{
   try
   {                            
       List<Product> listProducts = new List<Product>();                
       if (ModelState.IsValid)
       {                    
          db.Products.Add(product);                    
          db.SaveChanges();
          TempData["list"] = db.Products.ToList();                   
          return RedirectToAction("Index");
       }               
       return View(product);                          
    }
    catch
    {                
       return View(product);
    }
}

[HttpPost]
public ActionResult CreateInMemory(Product product)
{
   try
   {        
       if (ModelState.IsValid)
       {
           using (SQLiteConnection con = new SQLiteConnection("Data Source=:memory:"))
           {                          
               con.Open();                                                   
               if (string.IsNullOrEmpty(result.ToString()))
               {
                  string query = @"CREATE TABLE Products 
                                 (ProductID integer primary key,
                                  ProductNumber integer,
                                  ProductTitle varchar(100));";                             
                  using (SQLiteCommand comd = new SQLiteCommand(query,con))
                  {                               
                      comd.ExecuteNonQuery();
                      TempData["list"] = saveListProduct(product, con);
                   }
                }
                else
                {
                      TempData["list"] = saveListProduct(product, con);                           
                }
                      con.Close();
                      return RedirectToAction("Index");
         }
                    }                      
                    return View(product);
   }
   catch(Exception e)
   {
        string message = e.Message;
        return View("Index");
   }
}

In order that they are in context, I want to guard the model in the database and in memory with SQLite, and any suggestion is welcome.

Tomato32

I think you can use formaction attribute (HTML5) for this. Try the following. Hope to help, my friend :))

<input type="submit" name="response" value="Create" [email protected]("Create") formmethod="post" class="btn btn-default" />

<input type="submit" name="response" value="CreateInMemory" [email protected]("CreateInMemory") formmethod="post" class="btn btn-default" />

Note: It just can be implemented in HTML5.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass model from MVC view to angular controller?

MVC: How to pass model to partialview using viewbags from a different view?

How to pass model values from a view to a controller?

How to Pass Details Model from Controller to View

How to pass the model from view to controller?

pass a model from partial view to controller by ajax in mvc

MVC 5 pass model from view to controller using actionlink

How to pass custom model object from razor view to controller action in ASP.NET MVC 5

Pass Model from View to Controller

How to pass an id from a razor view to a controller's action method in ASP .Net MVC?

Sending a message to a view from a different method in controller in MVC

how to pass Html.HiddenFor from view to view and controller mvc

How to pass the different data from table view to another view controller

How to Pass variable between Different View Controller in iOS

Spring MVC pass model to view then to controller

how to pass data from one view controller to another and between the view controller there is a reveal view controller

Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller

how to pass entire model (with data) from view to controller?

How to pass data from view controller to data model in swift

How to pass Model List from Controller to View with Jquery or Javascript

How to pass model value IFormFile from view to controller On Change event

How to pass multiple objects of same model type from view to controller?

How to pass data from datepicker in view to controller in mvc

How to pass a variables data from a controller to a view (MVC)

ASP.Net MVC How to pass data from view to controller

How to pass row id by href link in mvc from view to controller

MVC 4 how pass data correctly from controller to view

How to pass List from view to controller - MVC 4

How to Pass value from view to another controller in MVC ASP .NET