简体   繁体   English

ASP.NET MVC & C#:将视图模型传递给控制器

[英]ASP.NET MVC & C# : pass a viewmodel to controller

I have a controller that passes the viewmodel to a view, and in the view it creates the table correctly.我有一个控制器将视图模型传递给视图,并在视图中正确创建表。

Then in the same page of the table I have a link that redirects the user to another page.然后在表的同一页面中,我有一个将用户重定向到另一个页面的链接。

This new page needs to keep the viewmodel of the previous view.这个新页面需要保留上一个视图的视图模型。 How could I do it?我怎么能做到?

In the view, in the table page this link that should redirect to an action of EanMatch controller:在视图中,在表格页面中,此链接应重定向到EanMatch控制器的操作:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  null, new {Vm = Model})

The model is this:模型是这样的:

public List<DDTViewModel> DDTList { get; set; }

In the EanMatchController and Index action I have:EanMatchControllerIndex操作中,我有:

[HttpGet]
public ActionResult Index(DDTListViewModel Vm)
{
    ....
}

I can't understand why it doesn't work.我不明白为什么它不起作用。 DDTListViewModel Vm in Index actions takes 'null' value, meanwhile I have seen with debug that Model got the list of data. Index操作中的DDTListViewModel Vm 取 'null' 值,同时我在调试中看到 Model 获得了数据列表。

How can I pass the viewmodel to the action argument correctly?如何将 viewmodel 正确传递给 action 参数?

Any doubt, ask !有疑问,追问!

For your particular case, You'll have to serialize your model as a JSON string, and send that to your controller to turn into an object.对于您的特定情况,您必须将模型序列化为 JSON 字符串,然后将其发送到控制器以转换为对象。

Here's your ActionLink :这是您的ActionLink

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  new { jsonModel= Json.Encode(Model.DDTListViewModel) }, null)

And your Controller method would look like:你的Controller方法看起来像:

public ActionResult Index(string jsonModel)
{
  var serializer= new DataContractJsonSerializer(typeof(Model.DDTListViewModel));
  var yourmodel=  (DDTListViewModel)serializer.ReadObject(GenerateStreamFromString(jsonModel));
}

if you controller name is EanMatch, then the next issue is that I think you are passing the model in where it expects htmlattributes.如果您的控制器名称是 EanMatch,那么下一个问题是我认为您正在将模型传递到它需要 htmlattributes 的地方。 So instead of null, new {Vm = Model};所以不是 null,而是 new {Vm = Model}; switch them.切换它们。

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch", new {Vm = Model} ,null )

Try this:尝试这个:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch",  null,  Model)

OR: Each request to the server has its own context(thread) and doesn't know anything about others.或者:对服务器的每个请求都有自己的上下文(线程)并且对其他请求一无所知。 If you need to share a model or another data between a few different requests to the server you can use temp data or session .如果您需要在对服务器的几个不同请求之间共享模型或其他数据,您可以使用temp datasession Inside of the first action, you need to save this model:在第一个操作中,您需要保存此模型:

Session["myModel"] = Model;

and read in another:并阅读另一个:

var val = Session["myModel"];

You don't need to create the anonymous object, just pass your view model:您不需要创建匿名对象,只需传递您的视图模型:

@Html.ActionLink("Inserimento manuale", "Index", "EanMatch", null, Model)

The framework will convert your model to a query string that will be bound by your controller.该框架会将您的模型转换为将由您的控制器绑定的查询字符串。

@Html.ActionLink("LinkName", "Index", "EanMatchController", new ViewModel { Id = 1, Name = "Sth", Description = "Hello" }, null);

You set the object as property to an anonymous object.您将对象设置为匿名对象的属性。 This is why your object is not passed on the controller.这就是为什么您的对象没有在控制器上传递的原因。

It is like having a wrapper object around your object like below.这就像在您的对象周围有一个包装对象,如下所示。

public class Sth{
 public ViewModel { get; set; }
}

So C# does not recognize this as your object you need to somehow create it inside razor and send it back to the controller.因此,C# 不会将此识别为您的对象,您需要以某种方式在 razor 中创建它并将其发送回控制器。 The code above works.上面的代码有效。 Try a simple example.尝试一个简单的例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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