简体   繁体   English

单击MVC / C#,从视图到控制器解析值

[英]Parse the value from view to controller on click MVC/C#

Since I am rendering 8 items and all of them are being assigned a number, I would like to parse that number to a model and than manipulate that data from the controller. 由于我正在渲染8个项目,并且所有项目都被分配了一个编号,因此我想将该编号解析为模型,而不是从控制器操作该数据。 I've tried this so far, but I am getting null reference exception. 到目前为止,我已经尝试过了,但是我得到了空引用异常。

 @{

        int[] myItems = { 1, 2, 3, 4, 5, 6, 7, 8 };
    }

    @foreach (var item in myItems)
    {

        <div class="beds col-lg-3 col-md-4 col-sm-6 col-xs-12">
            <h4>Bed: @item</h4> //System null reference
            <a data-toggle="modal" data-target="#myModal3">
                    <img src='@Url.Content("~/Images/AWT-Bed.png")' onclick="@Model.ItemNumber = @item" />
            </a>
        </div>
    }

I think you've misunderstood idea behind MVC and Razor templates as well. 我认为您也误解了MVC和Razor模板背后的想法。 Flow is as follows: 流程如下:

Server-side 服务器端

  1. Controller accepts data from View, operates, creates Model (or better called ViewModel/DTO as Model is often more complex idea involving datastorage solutions) Controller从View接收数据,进行操作,创建Model(或者更好地称为ViewModel / DTO,因为Model通常是涉及数据存储解决方案的更复杂的想法)

  2. That ViewModel is passed to the View Engine where it's rendered into final View that is passed to Client 该ViewModel传递给View Engine,在此它呈现为最终View并传递给Client

Client-side 客户端

  1. Client (web browser) displays View 客户端(Web浏览器)显示“视图”

  2. Client sends new data, possibly collected from View, to Controller on the Server 客户端将可能从View收集的新数据发送到服务器上的Controller

There's no direct link between them and you can't do this: 它们之间没有直接链接,您不能这样做:

onclick="@Model.ItemNumber = @item"

Server side and client side are separated via stateless communication chanel (HTTP protocol), without using sophisticated techniques, as websockets etc., there is no direct link between those two. 服务器端和客户端通过无状态通信通道(HTTP协议)分开,而无需使用复杂的技术(例如websocket等),这两者之间没有直接链接。 Server renderes View, based on ViewModel's data obtained from Controller, sends it to the Client and forgets about it. 服务器根据从Controller获取的ViewModel数据呈现View,然后将其发送到Client并忽略它。

I don't know what you're trying to achieve but try this: 我不知道您要达到什么目的,但是请尝试以下操作:

Controller on the server side 服务器端的控制器

public class ViewModel
{
    public int? SelectedItem { get; set; }
    public int[] MyItems { get; set; }
}

public class MyController : Controller
{
    [HttpGet]
    public ActionResult MyAction(int? selectedItem = null)
    {
        // Here you create your ViewModel and pass it to the View Engine to render
        ViewModel viewModel = new ViewModel()
        {
            SelectedItem = selectedItem,
            MyItems = { 1, 2, 3, 4, 5, 6, 7, 8 },
        };

        return View(viewModel);
    }
}

View, redenred on the server side but displayed on client's side 查看,在服务器端重新显示,但在客户端显示

    @model ViewModel

    <h3>Currently selected item is: @(Model.SelectedItem != null ? Model.SelectedItem.Value.ToString() : "none selected yet")</h3>

    @foreach (int item in Model.MyItems)
    {
            <div class="beds col-lg-3 col-md-4 col-sm-6 col-xs-12">
                    <h4>Bed: @item</h4>
                    <a data-toggle="modal" data-target="#myModal3">
                            <a href="@Url.Action('MyAction', new { selectedItem = item })"><img src='@Url.Content("~/Images/AWT-Bed.png")'/></a>
                    </a>
            </div>
    }

As you can see controller accepts input parameter "selectedItem" (which is null if action is called without parameters as when user first enters your page). 如您所见,控制器接受输入参数“ selectedItem”(如果在用户首次进入页面时不带参数的情况下调用动作,则为null)。 It prepares ViewModel which is passed to the ViewEngine. 它准备传递给ViewEngine的ViewModel。

There it's rendered. 在那里渲染。 We have loop which creates HTML with links and images. 我们有一个循环,可使用链接和图像创建HTML。 That HTML is then sent to the client's web browser. 然后将该HTML发送到客户端的Web浏览器。 If user click's on the image the browser calls your controller again, passing input parameter "selectedItem" which is again passed into the View and so on. 如果用户单击图像,浏览器将再次调用您的控制器,将输入参数“ selectedItem”传递给View,依此类推。

Of course it would benefit greatly from AJAX technique but this example shows base rule of web pages - those are static, stateless things. 当然,它将受益于AJAX技术,但是此示例显示了网页的基本规则-那些是静态的,无状态的东西。 You call action on the server, can pass parameters as input, get rendered result. 您在服务器上调用动作,可以传递参数作为输入,获得渲染结果。 No magic "assign that value to that model variable when user clicks something". 毫无疑问,“当用户单击某些内容时,将该值分配给该模型变量”。 Model (as everything) is temporary and exists only when controller's action is executed and view is rendered inside view engine. 模型(作为所有事物)是临时的,仅当执行控制器的操作并且视图在视图引擎中呈现时才存在。 After that it's send to the browser and everything is removed from memory. 之后,它将发送到浏览器,并且所有内容将从内存中删除。

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

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