简体   繁体   English

ASP.NET MVC:在不使用表单AND Ajax的情况下访问控制器中的绑定模型

[英]ASP.NET MVC: Accessing binded model in controller without using a form AND Ajax

I have the following scenario: 我有以下情况:

@model IEnumerable<UI.Models.Customer>

@grid.GetHtml(
columns: grid.Columns(
    grid.Column(
        format: @<text>
            @Html.ActionLink("Edit", "EditAct", ???)
        </text>
    ),
    grid.Column(
        format: @<text>
            @Html.TextBox("Name", (string)item.Name)
        </text>
        )
    )
)

model is just a List consisting of 3 Customer's (Customer is a dummy class with only 2 properties, ie ID and Name) 模型只是一个由3个客户组成的列表(客户是一个只有2个属性(即ID和名称)的虚拟类)

Now, i want to be able to capture in the controller action "EditAct "the Customer, which the user want's to edit. 现在,我希望能够在控制器动作“ EditAct”中捕获用户想要编辑的客户。 Those were the things, that i desperately tried without success: 这些就是我拼命尝试但没有成功的事情:


1) I tried to fill in ??? 1)我试图填写??? with a 与一个

new {customer: item}

and to capture it in controller this way: 并以这种方式在控制器中捕获它:

public ActionResult SampleFormParams(Customer customer) {...}

This approach does not work, because HTML anchor links uses GET and we can't send an object with GET. 这种方法行不通,因为HTML锚链接使用GET,而我们无法使用GET发送对象。


2) It tried to fill in ??? 2)它试图填写??? with a 与一个

new {customer: jsSerializer.Serialize(item)}

and to capture it in controller this way: 并以这种方式在控制器中捕获它:

public ActionResult SampleFormParams(String customer) { //deserialize(customer) }

This also does not work, because "item" is not a Customer object, but it is a WebGridRow object. 这也行不通,因为“ item”不是Customer对象,而是WebGridRow对象。


3) It tried to fill in ??? 3)它试图填写??? with a 与一个

new {customer: item.ID}

and to capture it in controller this way: 并以这种方式在控制器中捕获它:

public ActionResult SampleFormParams(int id) { ... }

I wanted to know if it would be possible to access the whole IEnumerable model, so that i can find the specified customer in the model using the given id ? 我想知道是否有可能访问整个IEnumerable模型,以便我可以使用给定的ID在模型中找到指定的客户?


So the question is, is there any way, that i can access model directly just with an anchor ie ActionLink 所以问题是,有什么办法,我可以直接使用锚即ActionLink直接访问模型
- without using a form> AND without using Ajax ? -不使用form> AND不使用Ajax吗?

I also checked the Custom Model Binder, but it is also based on a form>. 我还检查了自定义模型活页夹,但它也是基于form>。

Instead of generating edit link your way manually use a out of the box grid extension called getselectlink. 无需手动生成编辑链接,而是使用开箱即用的网格扩展名为getselectlink。 here is an sample: 这是一个示例:

grid.Column(
            header:"", 
            format:@<text>@item.GetSelectLink("Edit")</text>
            ),

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

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