Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 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 Answer

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...