0

I have tried multiple variations of this, but none of them seem to work. Any ideas?

Index.html:

@{
    var SWList = ViewData["SWList"] as List<Fostudio.Model.Software.Softwares>;
}

<div class="btn-group" role="group" aria-label="Basic example">
    <button type="button" class="btn btn-secondary" onclick="$.getScript('/Software/Index?SWType=文本编辑');">文本编辑</button>
</div>


<div class="row">
@{
    foreach (var item in SWList)
    {
        <div class="col-md-4" style="margin: 10px 0">
            <div class="card text-center">
                <div class="card-header">
                    @Html.DisplayFor(modelItem => item.Type)
                </div>
                <div class="card-block" style="margin: 10px 0;">
                    <h4 class="card-title" style="margin: 10px 0"> @Html.DisplayFor(modelItem => item.Name)</h4>
                    <p class="card-text" style="height: 40px;"> @Html.DisplayFor(modelItem => item.Description)</p>
                    <a href="@Html.DisplayFor(modelItem => item.URL)" class="btn btn-primary btn-sm" style="width: 150px; margin: 10px 0;">Get it</a>
                </div>
            </div>
        </div>
    }
}

<script type="text/javascript">
function ChangeSW(SWType) {
    $.ajax({
        url: '/Software/Index',
        type: 'GET',
        data: { SWType: SWType },
    });
}

SoftwareController:

public class SoftwareController : Controller
{
    private SoftwaresBLL softwareBLL = SoftwaresBLL.GetInstance();

    public ActionResult Index(string SWType = "")
    {
        if (SWType != "")
        {
            List<Softwares> SoftwaresList = softwareBLL.GetList("where Type=@0", SWType);
            ViewData["SWList"] = SoftwaresList;
            return RedirectToAction("Software", "Index");
        }
        else
        {
            List<Softwares> allSoftwaresList = softwareBLL.GetList();
            ViewData["SWList"] = allSoftwaresList;
            return View();
        }
    }
}

I want to be able to click a button and update the model list data shown in the view. I have been debugging and made sure the data is passed, but my view is not updated. I wanted to know if it is good practice to do that or if there is a better way to do it?

Thanks in advance.

4
  • Is there a reason you can't pass the model directly to the view using the View(model) function? If so, you could probably avoid the need for jQuery & ViewData here. Commented Oct 5, 2018 at 14:56
  • @Jack I didn't quite understand what you said. Could you be more specific? Thank you. Commented Oct 5, 2018 at 16:29
  • Take a look at this MSDN article that explains one way of how to pass a model to a view: learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/… Commented Oct 5, 2018 at 17:23
  • 1
    Ajax calls cannot redirect, so your return RedirectToAction("Software", "Index"); line of code makes no sense. And you not doing anything with the view you return back to the client - you need to update the DOM in the success callback Commented Oct 5, 2018 at 20:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.