0

Hi I have the following foreach that creates rows in a table.

_Summary.cshtml

@model KpiSummaryViewModel

...
@foreach (TimeIntervalAndStatus error in Model.MergedErrorTimeIntervals)
   {
      <tr>
         <td style="font-size: small" class="td downTimes">@error.TimeStart</td>
         <td style="font-size: small" class="td downTimes">@error.TimeEnd</td>
         <td style="font-size: small" class="td downTimes">@(error.TimeEnd - 
         error.TimeStart)</td>
         <td style="font-size: small" class="td downTimes">
            @Html.ActionLink("Details", "Index", "Details", error, null)
         </td>
      </tr>
   }

When I click the ActionLink above, I want to pass error but also KpiSummaryViewModel.SummaryInformation.Name to the Details Controller.

My DetailsController

public class DetailsController : Controller
{
   private TimeIntervalAndStatus _error;
   // GET: Details
   public ActionResult Index(TimeIntervalAndStatus error)
    {
        var detailsViewModel = new DetailsViewModel
        {
            QueryStartDate = error.TimeStart,
            QueryEndDate = error.TimeEnd
        };
        return View(detailsViewModel);
    }
}

My DetailsViewModel

public class DetailViewModel
{
   [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
   public DateTime QueryStartDate { get; set; }

   [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
   public DateTime QueryEndDate { get; set; }

   public TimeSpan DetailDownTimeDuration { get; set; }
}

My Details.cshtml

@model Foo.Models.DetailViewModel
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Down-time details</title>
    <script type="text/javascript">

    </script>
</head>
<body>
<div>
</div>
</body>
</html>

So, what I need is access to the error object the Html.ActionLink passes so i can set the fields on the DetailsViewModel to those TimeStart, TimeEnd and Duration. I will then use this information to retrieve other information. This part works, but I also need access to the @Model.SummaryInformation.Name. So I want to pass two things.

I don't want to add the Name to the TimeIntervalAndStatus as it doesn't make sense for that object to have the summary Name.

Perhaps I shouldn't be using an @Html.ActionLink, is there a better way to do this?

I have tried making my ActionLink this

@Html.ActionLink("Details", "Index", "Details", new { error, kpiName = Model.SummaryInformation.Name}, null)

Then my controller method becomes

public ActionResult Index(TimeIntervalAndStatus error, string kpiName)
{

}

But then error is always null.

8
  • What html generate after u try new {error, kpiName= Model....} Commented Apr 11, 2017 at 10:58
  • I get an object reference not set to an instance of an object, because error is null. Commented Apr 11, 2017 at 10:59
  • The url looks like this http://localhost:54816/Details?error=Foo.Models.TimeIntervalAndStatus&kpiName=APS Commented Apr 11, 2017 at 11:00
  • try this and check url new {TimeIntervalAndStatus = { TimeStart = error.TimeStart , TimeEnd = error.TimeEnd} , kpiName = Model.SummaryInformation.Name } Commented Apr 11, 2017 at 11:03
  • Thanks, I tried this. Now error is not null. But it has only the default values for DateTime. So TimeStart and TimeEnd are both {01/01/0001 00:00:00}. The kpiName is correct. Commented Apr 11, 2017 at 11:07

2 Answers 2

2

You should pass error Id and not error object in action link as it generates a Url. So change your View as

@Html.ActionLink("Details", "Index", "Details", new { errorId = error.Id, kpiName = Model.SummaryInformation.Name}, null)

And in your controller you can get related error object in action method using error id

public ActionResult Index(int errorId, string kpiName)
{
    // get error object using error id
}

Hope that solves your problem!

Sign up to request clarification or add additional context in comments.

4 Comments

Makes sense. And if errorId approach does not work you should flatten parameters and update view model to contain the Name property.
I don't have an id on error. In my other viewModel, I receive a list of times and TimeIntervalAndStatus is those times merged together to remove overlapping times. But really, all i need is kpiName, TimeStart and TimeEnd.
@Stuart, even in that case you don't need to build and pass object, you can pass dates as other parameters. i.e. new { startDate = error.TimeStart, endDate = error.TimeEnd, kpiName = Model.SummaryInformation.Name }. Add those as your action method parameters and build object there.
Yes, making it 3 parameters. Two DateTime and a string fixed it :)
0

You appear to be doing this ok. However your link should read

@Html.ActionLink("Details", "Index", "Details", new { error = error, kpiName = Model.SummaryInformation.Name}, null)

The error = error is needed to assign the error value (in your for loop) to enable your controller to receive it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.