Share via


How to access a variable in a partial view

Question

Wednesday, January 27, 2016 8:04 PM

I may be overthinking this but I can not figure it out. 

I have a ActionLink in my Index.cshtml that when clicked I need to access the @Title variable in my partial view. The variable is being set correctly but I am having issues passing to the partial view. 

My index.cshtml

@model IEnumerable<Canno.Models.Review>

@{
    ViewBag.Title = "Search";
}


<div class="panel panel-default">
  
    <div class="panel-heading"><h4>Select to review:</h4></div>
    <div class="panel-body">
        @using (Html.BeginForm("Index", "Review", FormMethod.Get))
        {
            <panel>

                <span class="glyphicon glyphicon-search"></span> Title: @Html.TextBox("SearchTitle")&nbsp;&nbsp;&nbsp;
               
                <br /><br />
                <input type="submit" value="Search" />
            </panel>
        }
    </div>
</div>
<br />




<div class="table-responsive">
    <div class="alert alert-info"><strong>Select to review</strong></div>
    <table class="table">
       
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Title)
            </th>
            
        </tr>
      
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.ActionLink(item.Title, "ViewConfirm",  "Review", new { @Title = item.Title }, new { @class = "modal-link"})
                 
                </td>
              
                    <td>
                        @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
                        @* @Html.ActionLink("Details", "Details", new { id = item.ID }) | *@
                        @Html.ActionLink("Delete", "Delete", new { id = item.ID })
                    </td>
              
            </tr>
        }

    </table>   
    <div style="clear: both;"></div>
</div>

partial View:

<div class="modal-body">
    <div class="alert alert-warning">
      
       Are you sure you want to review this: @Model.Title?
    </div>
    @using (Html.BeginForm("Confirm", "Review", FormMethod.Post))
    {
        <div class="row">
            &nbsp;
        </div>
        <div class="row">
            <div class="col-md-4 col-md-offset-4">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    Cancel
                </button>
                <button type="submit" id="approve-btn"
                        class="btn btn-danger">
                    Save
                </button>
            </div>
        </div>
    }
</div>
<script type="text/javascript">
    $(function () {
        $('#approve-btn').click(function () {
            $('#modal-container').modal('hide');
        });
    });
</script>

Controller:

   public ActionResult ViewConfirm()
        {
           

            return PartialView("_Confirm", new Canno.Models.Review());
        }


        [HttpPost]
        public ActionResult Confirm()
        {
            return RedirectToAction("Index");
        }

My model:

 public class Review
    {
      
        public string Title { get; set; }
        public int ID { get; set; }
       
     
    }

All replies (4)

Thursday, January 28, 2016 3:06 AM âś…Answered

Hi Gordon,

public ActionResult ViewConfirm() { return PartialView("_Confirm", new Canno.Models.Review()); }

You could change above code to the following.

public ActionResult ViewConfirm(string title)
{         
   var model= new Canno.Models.Review();
   model.Title= title;

   return PartialView("_Confirm", model);
}

Best Regards,

Chris Zhao


Wednesday, January 27, 2016 8:35 PM

Unless I'm misunderstanding what you're trying to do, and assuming 'Title' is a string, did you try adding 

@model String

to the top of your partial view? (and passing the string in from the controller?

Or alternatively, your controller is passing in a:    new Review()

and therefore the partialView first statement would be 

@model Canno.Models.Review

which you'd reference in the usual manner:

Are you sure you want to review this: @Model.Title?

Wednesday, January 27, 2016 9:07 PM

I tried both suggestions and the value in the partial view remains null. 


Wednesday, January 27, 2016 9:21 PM

That is because you are passing a 'New' object to your partial view.

You should initialize your object and then pass it to your partial view, like:

Canno.Models.Review review = new Canno.Models.Review()
review.Title = "Title";
review.Id = 1;
return PartialView("_Confirm", review);

This in your controller.