简体   繁体   中英

Pass parameter from a view to a controller

I essentially want to be able to pass a parameter to a view (which works) which will then be able to be passed further to a controller if the user presses a <a> link (which does not work as intended).

My initial view is defined as:

@model IEnumerable<Path.To.MyClass>
// Do stuff....
<a asp-controller="MyController" asp-action="MyView" asp-route-myProducts = "Model">Update</a>

Where my MyView within MyController is defined as:

[HttpGet]
public IActionResult MyView(IEnumerable<MyClass> myProducts)
{
//Insert a breakpoint 
}

Essentially I don't get any errors, but within the view in which I have my link myProducts has a bunch of elements. However when I access the breakpoint within MyView myProducts is empty.

Does anyone know what the issue may be?

I've tried to use [HttpPost] instead of [HttpGet] for MyView, but I seemed to have gotten an infinite loop when I ran the program using IIS Express then.

EDIT I've tried

<input type="button" onclick="location.href='@Url.Action("MyView", "MyController", Model)'">Update</button>

now as well with the same issue (the parameter for the controller is empty without any errors).

EDIT2: I've also tried making a new class like this:

public class ProductList
{
    List<MyClass> products {get;set;}
}

And consequently replaced the MyView parameter to be ProductList myProducts instead of IENumerable<MyClass> myProdcuts . In the @url.Action within the view I've tried to use <input type="button" onclick="location.href='@Url.Action("MyView", "MyController", Model.AsList())'">Update</button> as well. This gives the same issue (ie no error, but no elements within ProductList myProducts in my controller).

I will give an example of the general logic of posting information.

In the example I give, I pass a list of products to the controller.

model classes

public class ProductList
{
    List<Product> products { get; set; }
}
public class Product
{
   public int ID { set; get; }
   public string ProductName { set; get; }
}

in controller

[HttpGet]
public IActionResult MyView()
{
    var model = new ProductList();
    model.products = new List<Product>();
    Viewbag.Count = 5;
    return View(model);
}

[HttpPost]
public IActionResult MyView(ProductList model)
{
   foreach(Product item in model.products)
   {
     //do somethings
   }
   Viewbag.Count = 5;
   return View(model);
}

in view

@model yournamespace.ProductList
@{ 
   ViewBag.MTitle = "my view"; 
   Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@using (Html.BeginForm("MyView", "controllername", FormMethod.Post, new { enctype = "multipart/form-data"})) { 
  @Html.AntiForgeryToken() @Html.ValidationSummary(true)
  @for(var i = 0; i < Viewbag.Count; i++) {
        <div class="row">
           >@Html.TextBoxFor(model => model[i].ID)
        </div>
        <div class="row">
           @Html.TextBoxFor(model => model[i].ProductName)
        </div>
    }
    <div class="row">
       <button class="btn-control">add</button>
    </div>
}

If you want to pass a list with <a></a> ,you can try to serialize the model in view,and then deserialize the data to the model in action.Here is a demo:

Model:

public class ProductList
    {
        public List<MyClass> products { get; set; }
    }
    public class MyClass 
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

MyView1.cshtml:

@model Path.To.ProductList
@using Newtonsoft.Json;
<a href=@Url.Action("MyView","Test1",new {myProducts = JsonConvert.SerializeObject(Model) })>Update</a>

Controller:

[HttpGet]
        public IActionResult MyView1()
        {
            ProductList l = new ProductList { products = new List<MyClass> { new MyClass { Id = 1, Name = "class1" }, new MyClass { Id = 2, Name = "class2" }, new MyClass { Id = 3, Name = "class3" } } };
            return View(l);
        }
        [HttpGet]
        public IActionResult MyView(string myProducts)
        {
            ProductList p = JsonConvert.DeserializeObject<ProductList>(myProducts);
            return Ok();
        }

result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM