0

greeting again guys. First I have read over 20 post on this topic and at least 15 none stake exchange links as well as asked 5 question of my own regarding this.

First I just want to know can mvc populate a partial view with dynamic data while in the create view? If not then what I suspected was correct. This can only be down in webforms and I can drop this mvc app and just use webforms.

If it can can some one please show how to do this. Again here is everything that I have.

stating with the partial view code called

_PartVue that is stored in the shared folder of the mvc code behind folders this part view was build using the wizard

<table cellpadding="1" border="1">
    <tr>
        <th>
            Field1
        </th>
        <th>
            Field2
        </th>
        <th>
            Field3
        </th>

   </tr>

    @foreach (System.Models.DATA_LIST item in @ViewBag.List)
    {
        <tr>
            <td>
                @item.F1
            </td>
            <td>
                @item.F2
            </td>
            <td>
                @item.F3
            </td>
        </tr>
    }

</table>

In the Create View

This is where in the form that I want the partial view to show up on the create form

        <div class="col-sm-6">

            <div class="form-horizontal" style="display:none" id="PV_List">

                @{ Html.RenderAction("ShowList",);}


            </div>
        </div>

The java script code in the create view for the list to show after a drop down view is changed

    $(document).ready(function () {
        $('#RES_ID').change(function ()
        {
            debugger;

            $.get('~/Views/Shared/_PartVue.cshtml', { VID: $(this).val() }, function (data) { $('#PV_List').html(data); });
            $("#PV_List").show(); // Shows Partial View


        });
    }

in the controller I have a function that makes a list and a function that uses something called return PartialView:

    [HttpGet]
    public ActionResult ShowList(int? VID)
    {


        ViewBag.DataList = Get_List(VID);
        return         PartialView("~/Views/Shared/_PartVue.cshtml",ViewBag.DataList);


    }

Also the Get List function that populates the view bag data

    private List<LIST_DATA> GET_List(int? VID)
    {

        return db.LIST_DATA.Where(i => i.ID == VID).ToList();
    }

What I get on the create from are the fields but with no data. WHen is step into the code behhind I see that the right data is selected but it does not go no the partial view form.

Here is what I get.

In the google browser I get the following error: http://localhost:50296/PrograX/~/Views/Shared/_PartVue.cshtml?VID=808 Failed to load resource: the server responded with a status of 403 (Forbidden)

This partial view is in the shared folder... so is mvc telling me thatit can't populate a partial view with dynamic data and that I need to use webforms instead of trying to use mvc?

thanks so much again. Hopefully this question better explain what it is I am trying to do and maybe I can get better guidance letting me know that it is not possible and that it can't be done in this platform. Thanks

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • You need to delete this and edit your [previous question](http://stackoverflow.com/questions/41967954/how-do-you-pass-a-viewbag-to-a-partial-view). And before you ask any further questions, carefully read the [help files](http://stackoverflow.com/help) to understand how to ask a question on this site (your dangerously close to getting a ban). –  Jan 31 '17 at 23:58
  • Hi @StephenMuecke what do you mean? I need to delete the questions? Why would you ban me? –  Feb 01 '17 at 00:02
  • I can't ban you, but the system will because of you poor (down-voted) questions. And you cannot just keep repeating questions. Edit the previous one with the relevant details as I had requested, or delete the previous one. –  Feb 01 '17 at 00:05
  • @StephenMuecke I just read thru the questions and this one is different than the other other... they are similar but this one is more correct. But thanks for the advice. If they ban me they ban me... but I think I have my answer that it can't be done so I guess it wont really matter. –  Feb 01 '17 at 00:10
  • 1
    If course it can be done - its `$.get('@Url.Action("ShowList")', { VID: $(this).val() }, function (data) { ` assuming the `ShowList()` method is in the same controller as the GET method that generated the main view. And its `var model = Get_List(VID); return PartialView("_PartVue", model);` an in the partial view `@model List` and `foreach (var item in Model) { ... }` –  Feb 01 '17 at 00:14
  • @StephenMuecke @ model List? Where does that go sir? Can you just point me to a tutorial that shows what you have typed as I can;t tell where theses elements go im my code... I think you are trolling me sir. I have no idea where all these code bits go sadly. Again thanks for trying to help. –  Feb 01 '17 at 00:19
  • At the top of the view. Sorry but its clear you have no understanding of MVC and you need to go to the MVC site and work through the tutorials to learn the basics as I noted in your previous question. –  Feb 01 '17 at 00:20
  • 1
    @StephenMuecke I never said I understood mvc all that well. I said I tried all those things... you have something called a model in you code... I don't I have a view bag as I don't understand the whole model thing. I kinda understand view bags because it got my dropdown list to work using that concept. I would be grateful to you if you could point me to the site that has this partial view thing working as I need details or if you think it can be done can you just add an answer with the code listed out they way it should be?? The comments are hard to read and just add to the confusion for me. –  Feb 01 '17 at 00:22
  • @StephenMuecke I disagree but also I can't ask additional questions there as I don't have the rank. So there may be a green check but I can't talk to the people as you have to have more points I guess to add a comment to ask for clarity some maybe you can untag this question as answered so people that may know if what I am asking is even possible may chime in freely. Currently It appears that mvc can do dynamic passing of data. I just need to hear from a expert confirming what I believe to be true. Can you untag this please? Thanks so much for understanding. –  Feb 01 '17 at 14:59
  • @StephenMuecke Also that answer is using a model . My question is about viewbags not models as I don't understand that because my create page already has a model. So I can't have more than one data model. This is a question about viewbags and partial views sir. –  Feb 01 '17 at 15:07
  • The question has got nothing to do with `ViewBag` or models (that makes no difference at all). The question is about why you get the `403 (Forbidden)` error which the dupe explains (and if you change `$.get('~/Views/Shared/_PartVue.cshtml', ....)` to `$.get('@Url.Action("ShowList")', ... )` you will no longer get that error! –  Feb 01 '17 at 20:49
  • @StephenMuecke I did make the change it still fails. it can't be done so I am quitting that attempt. As I am wasting time with a feature that is some that mvc can;t handle. This thing that I want to do appears to be something that can only happen in webforms... mvc seems to need data and can;t render an empty partial view. I have moved to trying to just use a static partial view and that fails too. So I guess create can use any kind of partial view. I am just blown away that something seeming so simple can't be down. Even in the threads yous sent me two the users are doing some very different –  Feb 01 '17 at 20:57
  • Of course it can be done. You can do far more in MVC that you could do in web forms (and if you made that change you will not get a `403 (Forbidden)` meaning the dupe did solve the problem in your question) –  Feb 01 '17 at 21:02
  • @StephenMuecke no sadly it give another error... again the computer is telling it can't be done. Object reference not set to an instance of an object. Description: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Line 17: @ foreach (System.Models.LIST_FULL item in @ ViewBag.FuLL_List) this is because the list is empty as the create view has no value to pass the list. Which means mvc can't do this on a create view is my take away. –  Feb 01 '17 at 21:23
  • So you saying that it did solve the `403 (Forbidden)` because you no longer get that error meaning it is a dupe. And the `NullReferenceException` is obvious - the value of `ViewBag.FuLL_List` is `null` (its not empty) and in any case you should be casting the `ViewBag`. And all you would need to solve that is to check if its `null` first. –  Feb 01 '17 at 21:28
  • @StephenMuecke I am not saying anything... as I don't know how to stop the partial view from rendering if the list is null. I tried .count and all those things but they way it's setup the create view is still trying to render the bloody thing. I don't know if mvc has some special command. My guess is that on a create view since the list is going to be initially null its has to draw the partial view... who knows. I should have done this bloody app in webforms!! I would have had this licked in 10 seconds! –  Feb 01 '17 at 21:34
  • This question has gotten into rather a bad state, to the degree that it is no longer a question. It is an article, or perhaps a blog post. In general we encourage posters to self-answer, which you did do, but your answer was deleted because it exactly duplicated an answer you made elsewhere. Could you edit your answer into the duplicate question? – halfer Feb 11 '17 at 16:39

1 Answers1

0

First of all, You can specify which model you want to use in your partial view like this :

@model List <YourSolutionName.YourFolderNameWhereModelIs.YourModelName>

then you can use it in foreach as follows:

@if(Model.Count > 0)
{
 foreach(var item in Model)
 {
        <tr>
            <td>
                @item.F1
            </td>
            <td>
                @item.F2
            </td>
            <td>
                @item.F3
            </td>
        </tr>
   }
}
Utkarsh Bais
  • 197
  • 8
  • Hi and thanks sir.. Yes that is what I ended up doing as I did not know you could have If statements in the HTML code. Many thanks for your help sir! –  Feb 01 '17 at 23:28
  • 1
    Most Welcome ! MVC has a steep learning curve. And don't forget to mark the comment as Answer, it will help all the others looking for the same thing. – Utkarsh Bais Feb 02 '17 at 08:02