3

I have project where when I insert something I want to refresh the partial view only or table only. When I click the button to insert it does insert but does not load the table, the table will just appear empty

View:

@model ClinicManagemet.Models.Assessment

@{
ViewBag.Title = "Update Assessment";
}
<script src="~/Scripts/jquery-3.1.1.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<h2>Assessment</h2>
<script>
    $(document).ready(function () {
        $('#btn-disease').click(function () {
            var diseaseID = $('#DiseaseID').val();
            var assessmentID = $('#AssessmentID').val();
            var urll = '/DiseaseLists/_DiseaseList?id=' + assessmentID;
            $.ajax({
                type: "POST",
                dataType: "Json",
                data: {
                    'diseaseID': diseaseID,
                    'assessmentID': assessmentID
                },
                url: '@Url.Action("CreateDisease", "DiseaseLists")',
                success: function (f) {
                    $('#tbl-disease').load(urll);
                    alert(f);
                }
            })
        })
    })
</script>
@Html.HiddenFor(model => model.AssessmentID)
<div class="form-group">
    @Html.LabelFor(model => model.DiseaseID, "DiseaseID", htmlAttributes: new { @class = "control-label col-md-2" })

    <div class="col-md-10">
        @Html.DropDownList("DiseaseID", null, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.DiseaseID, "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" id="btn-disease" value="Add" />
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2W col-md-10">
        <div id="tbl-disease">
            @{
                Html.RenderAction("_DiseaseList", "DiseaseLists", new { Model.AssessmentID });
            }
        </div>
    </div>
</div>

partial view controller:

public ActionResult _DiseaseList(int? assessmentID)
    {
        var diseaseLists = db.DiseaseLists.Include(d => d.Assessment).Include(d => d.Disease).Where(d => d.AssessmentID == assessmentID);
        return PartialView(diseaseLists.ToList());
    }
6
  • 1
    try like this $('#tbl-disease').load( '@Url.Action("_DiseaseList", "DiseaseLists",new {assessmentID = assessmentID})' ); Commented Jun 21, 2017 at 5:47
  • 1
    @Curiousdev it worked smoothly. thanks Commented Jun 21, 2017 at 5:49
  • 1
    @Mark no it'll not return HTML the ajax in example is referring different Action @Url.Action("CreateDisease", "DiseaseLists") Commented Jun 21, 2017 at 5:49
  • @kielou great :) Commented Jun 21, 2017 at 5:49
  • 2
    Further references: jQuery.load() & jQuery.html(). Commented Jun 21, 2017 at 5:54

1 Answer 1

3

As Curiousdev said in the comment, I just changed some code in my jquery

$('#tbl-disease').load( '@Url.Action("_DiseaseList", "DiseaseLists",new {assessmentID = assessmentID})' );
Sign up to request clarification or add additional context in comments.

2 Comments

Please Marked yor answer as accepted for future references
@Curiousdev I will, but I need to wait 2 days

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.