简体   繁体   中英

Binding the Model variable to an Action Method in ASP.NET MVC3

I have a controller HomeController with the following action method :

[HttpPost] 
public ActionResult DisplayData(MyViewModel myViewModel)
{
   // Do something with myViewModel           
}

The ViewModel :

public class MyViewModel
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public bool IsPeriod { get; set; }
}

And the following View

@model AppName.ViewModels.MyViewModel

@{ Html.RenderPartial("MyPartialView", Model);  }

<img src="@Url.Action("DisplayData", "Home", new { myViewModel = Model })" alt="Image" />

I use the Url.Action how it is described here but what I get in the DisplayData action method is null. In the source code I got:

<img src="/Home/DisplayData?filters=AppName.ViewModels.MyViewModel" alt="Image" />

so it is passing actually the type instead of the values.

The ViewModel instead is correctly passed to the partial view. What am I doing wrong?

I usually add the following to my model:

public class MyViewModel
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public bool IsPeriod { get; set; }

    public RouteValueDictionary RouteValues
    {
        get
        {
            var rvd = new RouteValueDictionary();
            rvd["name"] = Name;
            rvd["surname"] = Surname;
            rvd["isPeriod"] = IsPeriod;
            return rvd;
        }
    }
}

Then you can simply use the RouteValues property in your Url.Action() call.

<img src="@Url.Action("DisplayData", "Home", Model.RouteValues)" alt="Image" />

Or if your prefer less (explicit) code, ignore the model changes and simply do this:

<img src="@Url.Action("DisplayData", "Home", new RouteValueDictionary(Model)" alt="Image" />

It will not serialize your entire object to the query string you must set each value explicitly, ie

new { nameFilter = @Model.Name, @Model.Surname, @Model.IsPeriod }

You only need provide nameFilter if your parameter is not called Name in your routing.

You are trying to create an img src link using an entire view model, I don't think this is really what you want.

Instead, you would pass the Id of whatever the model represents as a query string parameter on the link (filters). So for example using...

<img src="@Url.Action("DisplayData", "Home", new { filters = Model.Id })" alt="Image" />

To render something like...

<img src="/Home/DisplayData?filters=1" alt="Image" />

Then you can use the Id in your action to look up whatever the resource is you are trying to display.

If you want to pass model the way you described then you must override ToString() method of your ViewModel class, because this is the reason why passing your ViewModel object to Url.Action method returns such a link. Also, if you want to pass your ViewModel to Action, you must prepare link in the form of "http://url/Action?Name=x&Surname=y&IsPerion=0" so ModelBinder can recognize it and bind to Action parameter.

你试过这个吗?

<img src="@Url.Action("DisplayData", "Home", Model)" alt="Image" />

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