简体   繁体   English

从PartialView到Controller的模型返回null

[英]Model from PartialView to Controller returns null

I have a PartialView that recibes a Model ID and then when you click on the link sends the Model ID to the Controller. 我有一个PartialView,它记录一个Model ID,然后在您单击链接时将Model ID发送给Controller。 My problem was that there were 2 ways to send data from View to Controller but just only one worked for me. 我的问题是,有两种方法可以将数据从View发送到Controller,但只有一种对我有用。 What's the reason why? 到底是什么原因

This worked perfectly for me 这对我来说很完美

<a type="button" class="btn btn-primary" asp-controller="Dictionary" asp-action="Edit" asp-route-wordId="@Model"><i class="fas fa-edit"></i></a>

This one didn't work 这个没用

<a type="button" class="btn btn-success" href="@Url.Action("Edit/" + Model)"><i class="far fa-list-alt"></i></a>

My question is why the last one didn't work and returned null if I am passing the Model ID to the URL 我的问题是,如果我将模型ID传递到URL,为什么最后一个不起作用并返回null

This is my full PartialView that receives a Model ID and works 这是我的完整PartialView,可以接收模型ID并正常工作

@model int

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<td style="width:150px">
    <div class="btn-group" role="group">
        <a type="button" class="btn btn-primary" asp-controller="Dictionary" asp-action="Edit" asp-route-wordId="@Model"><i class="fas fa-edit"></i></a>
    </div>
</td>

And this is my Controller Function that recibes the data 这是我的控制器功能,可以记录数据

public async Task<IActionResult> Edit(int? wordId)
{
    if(wordId == null)
    {
        return NotFound();
    }

    var word = await _db.Dictionaries.FindAsync(wordId);
    if(word == null)
    {
        return NotFound();
    }

    return View(word);
}

The Url.Action overload you are using takes in an action name and you are passing something like /Edit/4 to it. 您正在使用的Url.Action重载采用一个操作名称,并且您要向其传递类似/Edit/4 You need to use the proper overload of the Url.Action method that allows you to specify route values. 您需要使用允许您指定路由值的Url.Action方法的适当重载。

It would look something like the following: 它看起来像以下内容:

<a type="button" class="btn btn-success" href="@Url.Action("Edit/" + Model)"><i class="far fa-list-alt"></i></a>

to

<a type="button" class="btn btn-success" href="@Url.Action("Edit", new { wordId = Model } )"><i class="far fa-list-alt"></i></a>`

As a side note, your Edit action takes a nullable wordId which doesn't make much sense to me. 附带说明一下,您的Edit操作采用可为空的wordId ,这对我来说没有太大意义。 Generally, if you're editing something then it has already been created and should always have an id . 通常,如果您正在编辑某些内容,则该内容已经创建,并且应始终具有id The only case I can think of where this makes sense is if the Edit action actually handles creating data as well as editing data in which case that would be perfectly valid. 我能想到的唯一情况是,如果Edit动作实际上处理创建数据以及编辑数据的情况,那将是完全有效的。

To answer your main question, the reason your 1st anchor is working is because the args are correct. 要回答您的主要问题,您的第一个锚正常工作的原因是因为参数正确。 For example: 例如:

<a asp-controller="Dictionary" asp-action="Edit" asp-route-wordId="1">Your link</a>

is equivalent to: 等效于:

<a href="/Dictionary/Edit/1"></a>

That works! 这样可行!

The second will not work because you are misusing Url.Action , the method you are using is this Url.Action("Action") 第二个将不起作用,因为您滥用Url.Action ,您正在使用的方法是此Url.Action("Action")

Url.Action("Edit/" + Model) or  Url.Action("Edit/1") 

is searching for an action named: "Edit/1", which in your case will not be found. 正在搜索名为“ Edit / 1”的操作,在您的情况下将找不到该操作。

so in your 2nd not working example 所以在你的第二个不工作的例子

<a href="@Url.Action("Edit/" + Model)">Your Link</a>

is equivalent to: 等效于:

<a href="null" />

In your case you should be using this in your anchor: 在您的情况下,您应该在锚点中使用它:

Url.Action("Edit", "Dictionary", new { id = @Model })

is equivalent to /Dictionary/Edit/1 等效于/ Dictionary / Edit / 1

so: 所以:

<a href="@Url.Action("Edit", "Dictionary", new { id = Model })">Your link</a>

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

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