Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

controller - Move data from a view to another view

I would like to move a data from an IActionResult to another IActionResult. I have stored the data of the product details in return View (cdd). How do I stored it into Tempdata in order for it to pass it to another view? Here are my codes:

[HttpGet]
    public IActionResult Details(int id)
    {
        string sql = String.Format(@"SELECT * FROM WBProduct 
                               WHERE Id = {0}", id);
        List<Product> lstProduct = DBUtl.GetList<Product>(sql);

        if (lstProduct.Count == 0)
        {
            TempData["Message"] = $"Product #{id} not found";
            TempData["MsgType"] = "warning";
            return Index();
        }
        else
        {
            Product cdd = lstProduct[0];
            return View(cdd);
            

            
        }
    }

I would like to call the data 'cdd' in this action method.

public IActionResult Create(Product product)
    {
        return View("Create", product);
    }

Here is my view for Details Action Method:

@model List<Product>

@if (TempData["Message"] != null)
{
 <div class="alert alert-@TempData["MsgType"]">
    @TempData["Message"]
</div>
}

<table class="table table-condensed table-hover">
<tr>
    <th scope="col">ID</th>
    <th scope="col">Product</th>
    <th scope="col">Price</th>
    <th scope="col">Quantity</th>

</tr>


<tr>
    <td>@ViewData["ID"]</td>
    <td>@ViewData["Product"]</td>
    <td>@ViewData["Price"]</td>
    <td>@ViewData["Quantity"]</td>
    <td>
        <a asp-controller="Product"
           asp-action="Delete">
            Delete
        </a>
    </td>

    <td>
        <a asp-controller="Product"
           asp-action="Checkout">
            Checkout
        </a>
    </td>

</tr>
question from:https://stackoverflow.com/questions/65649522/move-data-from-a-view-to-another-view

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, the page model in your Details view should be Product insteat of List<Product>.

@model Product

Since cdd is a record from your database table, you could set a anchor in Details.cshtml to send the id to Create action. Then get the product from database like that in Details action.

Details.cshtml:

<a asp-controller="Product" asp-action="Create" asp-route-id="@ViewData["ID"]">
        Create
</a>

Create:

[HttpGet]
public IActionResult Create(int id)
{
    string sql = String.Format(@"SELECT * FROM WBProduct 
                           WHERE Id = {0}", id);
    List<Product> lstProduct = DBUtl.GetList<Product>(sql);
    Product cdd = lstProduct[0];
    return View("Create", product);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...