0

In my HomeController, I defined:

static void send_data_to_log(ref DateTime last_time)
{
   // do something
 }

in my index of the controller, I defined:

DateTime last_time = DateTime.Now;
ViewBag.my_last_time = last_time;
send_data_to_log(ref last_time);

now I want to use this function and this variable(last_time) in my index.cshtml file.

I tried:

<input type="hidden" name="" id="my_last_time" value="@ViewBag.my_last_time" />
@send_data_to_log(ViewBag.my_last_time);

but I got error :/

I want that the viewbag.my_last_time and the last_time will be updated in the function.

any help appreciated!

4

1 回答 1

3

MVC用于separation of concerns. 在视图页面上有逻辑是非常糟糕的主意。您可以将日期发送到控制器并调用控制器中的函数。

您可以序列化您的日期,然后使用AJAX, 将其发送到控制器并在控制器中调用这些函数。

于 2013-06-18T15:47:27.543 回答