简体   繁体   English

将值从视图传递到控制器-MVC

[英]Pass Value from View to Controller - MVC

i have a list of thumbnails for the user can select one of the image. 我有一个缩略图列表,用户可以选择其中一张图像。

onclick on the thumbnail open a larger image into a form. 在缩略图上单击,将较大的图像打开成表格。

What im trying to do now is send the id of the image selected to my controller. 我现在想做的就是将所选图像的ID发送给我的控制器。

Note: im using MVC 4. 注意:即时通讯使用MVC 4。

how can i do that? 我怎样才能做到这一点?

someone can help with this pls? 有人可以帮忙吗?

Thanks in advance: 提前致谢:

Here is my code: 这是我的代码:

 @foreach (var p in ViewBag.Images)
    {           
     <li>
       <a href="~/Files/@p.Name" onclick="swap(this); return false;">
          <img src="~/Files/@p.Name"/>
       </a>
     </li>
}

when selected is going this img tag in my form: 当选择时将以我的形式使用此img标签:

<img id="main" src="" >

using this javascript for this event: 为此事件使用此javascript:

function swap(image) {
        document.getElementById("main").src = image.href;
    }

what i have to do now? 我现在要做什么?

i trying with <input type="hidden" name="Img_Id" value="Viewbag.??????"/> to pass this value to my controller?? 我尝试使用<input type="hidden" name="Img_Id" value="Viewbag.??????"/>将此值传递给我的控制器?

First, some terminology help: You can't pass a value from the view to the controller action, the view is rendered after the controller action completes. 首先,一些术语帮助:您不能将值从视图传递给控制器​​动作,视图将在控制器动作完成后呈现。

What you want to do is pass data from the client (web browser) to a controller action, using form fields. 您要做的是使用表单字段将数据从客户端(Web浏览器)传递到控制器操作。

In your javascript swap method, you could set the value of the Img_Id field to be the value for the selected image. 在您的JavaScript交换方法中,您可以将Img_Id字段的值设置为所选图像的值。 When the form is submitted, the Img_Id will be posted as form data, and can be accepted as a parameter in the action. 提交表单后,Img_Id将作为表单数据发布,并且可以作为操作中的参数接受。

You can use JQuery (or something else) to perform the client side actions. 您可以使用JQuery(或其他方式)执行客户端操作。

Here's an example (not tested though!): 这是一个示例(虽然未测试!):

First add the ID as a data attribute on the element: 首先,将ID作为元素的data属性添加:

<a href="~/Files/@p.Name" data-id="@p.ID" onclick="swap(this); return false;">

Then some javascript to save that to form (using jquery here): 然后使用一些JavaScript将其保存为表单(在此处使用jquery):

function swap(image) {
    document.getElementById("main").src = image.href;
    $("input[name='Img_Id']").val($(image).data("id"));
}

To pass a value back to your controller, you either need to submit a form, or else make an AJAX request to your controller. 要将值传递回控制器,您要么需要提交表单,要么向控制器发出AJAX请求。

In the first case, you'd need to update the value of your hidden field with javascript, and then either wait for the user to submit the form, or trigger a submit through javascript depending on what your needs are. 在第一种情况下,您需要使用javascript更新隐藏字段的值,然后等待用户提交表单,或根据需要触发通过javascript提交。

If you want to do an ajax request, it would be more or less the same thing, but you don't need a hidden field to store the value. 如果要发出ajax请求,则差不多是同一件事,但是不需要隐藏字段来存储值。

You could use jQuery in your swap function. 您可以在交换函数中使用jQuery。 See here for the official documentation. 请参阅此处获取官方文档。

If you chose to use this approach, and assuming you place your JavaScript in a separate file, then make sure you get the path for the action and controller and pass that in too. 如果选择使用这种方法,并假定将JavaScript放在单独的文件中,则请确保获取动作和控制器的路径并将其也传递进来。

var url = @Url.Action("Index","Home");

Therefore you may call: onclick="swap(this.id, url)" 因此,您可以致电:onclick =“ swap(this.id,url)”

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

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