0

I want to add a '+' symbol below the table that shows the content of a particular table enabling the user to add another row of data to the table. I want the data to be saved in the database.

I tried integrating my two current views together but it doesn't work.

Here is my View for viewing the table. Here is the place where I want the '+' Symbol to add new values.

@model IEnumerable<TCImplementation.Models.TCSet>
<table class="table">
  <tr>
    <th>@Html.DisplayNameFor(model => model.ValueName)</th>
    <th>@Html.DisplayNameFor(model => model.DataFormat.FormatName)</th>
    <th>@Html.DisplayNameFor(model => model.TC.TCName)</th>
    <th>@Html.DisplayNameFor(model => model.DataUsage)</th>
    <th>@Html.DisplayNameFor(model => model.DataStatus)</th>
    <th></th>
  </tr>
  @foreach (var item in Model) {
    <tr>
      <td>@Html.DisplayFor(modelItem => item.ValueName)</td>
      <td>@Html.DisplayFor(modelItem => item.DataUsage)</td>
      <td>@Html.DisplayFor(modelItem => item.TC.TCName)</td>
      <td>@Html.DisplayFor(modelItem => item.DataFormat.FormatName)</td> 
      <td>@Html.DisplayFor(modelItem => item.DataStatus)</td>
      <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.TCSetID }) |
        @Html.ActionLink("Details", "Details", new { id=item.TCSetID }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.TCSetID })
     </td>
    </tr>
  }
</table>
@Html.ActionLink("Create New", "Create", new {id = ViewBag.id })

In the Html.ActionLink() I want to pass the id of the parent table. My create function takes parent id as a parameter. But ViewBag.id doesn't help as well.

What is the work around for my task?

EDIT 1

Addition of the Create View

@model TCImplementation.Models.TCSet
@using (Html.BeginForm()) 
{
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.ValueName, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EditorFor(model => model.ValueName, new { htmlAttributes = new { @class = "form-control" } })
  @Html.ValidationMessageFor(model => model.ValueName, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataUsage, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EnumDropDownListFor(model => model.DataUsage, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataUsage, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataStatus, htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.EnumDropDownListFor(model => model.DataStatus, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataStatus, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.TCID, "TCID", htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.DropDownList("TCID", null, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.TCID, "", new { @class = "text-danger" })

  @Html.LabelFor(model => model.DataFormatID, "DataFormatID", htmlAttributes: new { @class = "control-label col-md-2" })
  @Html.DropDownList("DataFormatID", null, htmlAttributes: new { @class = "form-control" })
  @Html.ValidationMessageFor(model => model.DataFormatID, "", new { @class = "text-danger" })

  <input type="submit" value="Create" class="btn btn-default" />
}
@Html.ActionLink("Back to List", "Index")

@section Scripts {
  @Scripts.Render("~/bundles/jqueryval")
}

EDIT 2 : Adding Create Action method

    public ActionResult Create(int id)
    {
        ViewBag.id = id;
        ViewBag.DataFormatID = new SelectList(db.DataFormat, "DataFormatID", "FormatName");
        ViewBag.TCID = new SelectList(db.TC, "TCID", "TCName",id);
        return View();
    }

EDIT 3 - Adding model classes

public class TC
{
    public int TCID { get; set; }
    public string TCName { get; set; }
    public virtual ICollection<TCSet> TCSets { get; set; }

}



public class TCSet
{
    public int TCSetID { get; set; }
    public string ValueName { get; set; }
  //  public string DataFormat { get; set; }
    public DataUsage DataUsage { get; set; }
    public DataStatus DataStatus { get; set; }
    public int TCID { get; set; }
    public int DataFormatID { get; set; }
    public virtual TC TC { get; set; }
    public virtual DataFormat DataFormat { get; set; }
}

EDIT 4: ViewTCSet for a particular TCID

    public ActionResult ViewTCSet(int ?id)
    {
        var viewmodel = new TC_TCSet();

        if(id!=null)
        {
            ViewBag.TCID = id.Value;
            var tcSet = db.TC.Include(x => x.TCSets).FirstOrDefault(x => x.TCID == id);
            if(tcSet!=null)
            {
                viewmodel.TCSet = tcSet.TCSets;
            }
        }
        return View(viewmodel);
    }

View for ViewTCSet

@model TCImplementation.ViewModels.TC_TCSet
@{
ViewBag.Title = "ViewTCSet";
}
<table>
<tr>
    <th>Tc Set Name</th>
    <th>Data Usage</th>
    <th>Data Status</th>
    <th>Data Format</th>

</tr>
@foreach(var item in Model.TCSet)
{
    <tr>
        <td>@item.ValueName</td>
        <td>@item.DataUsage</td>
        <td>@item.DataStatus</td>
        <td>@item.DataFormat.FormatName</td>
        <td>@Html.ActionLink("Edit", "Edit", "TCSets", new { id= item.TCSetID},null) | @Html.ActionLink("Details", "Details", "TCSets", new { id = item.TCSetID }, null) | @Html.ActionLink("Delete", "Delete", "TCSets", new { id = item.TCSetID }, null)</td>
    </tr>
}
</table>
@Html.ActionLink("Create", "Create", "TCSets", new { id = Model.TCSet }, null)

In this actionlink I am unable to pass the value Model.TCSet.TCID

tereško
  • 58,060
  • 25
  • 98
  • 150
Vini
  • 1,978
  • 8
  • 40
  • 82
  • So your `Create` action has one input parameter `id`, yes? Do you call something like this `ViewBag.id = ####;` in the action for the posted view? – xxxmatko Sep 18 '15 at 08:05
  • in my code, the create function for 'TCSet' is called from 'TC'. The value for the ViewBag.id is initialized in the create function. I need to pass the same value over and over when the create is called from the view that is mentioned above. – Vini Sep 18 '15 at 08:16
  • Yes, it always accepts input paramenter id and i have `ViewBag.id=id` in the create action method. – Vini Sep 18 '15 at 08:18
  • Could you post the body of your create action? – xxxmatko Sep 18 '15 at 08:45
  • I have posted the view of the create action. do you want the the action method as well?? – Vini Sep 18 '15 at 08:48
  • _But ViewBag.id doesn't help_? Why not?. If your GET method that generates the first view has `ViewBag.Id = someValue;` then it will be passed to the view, and in turn to the `Create` view. And _I tried integrating my two current views together but it doesn't work._ - of course it can work - what did you try? –  Sep 18 '15 at 11:09
  • @Stephen Muecke: I dont know how i could integrate two views when the model for each views are different. either one of the models can only be passed to the View. I cant understand a point making a viewmodel in this scenario. And the ViewBag.id is initiated when the create is called for the first time. As i want the same id to be passed over again and again i tried calling the create function from the '+' symbol by passing ViewBag.id. But the value is not being accepted in side the function. – Vini Sep 19 '15 at 07:29
  • @xxxmatko: I am trying to create the create method a second time. i will post it soon. – Vini Sep 19 '15 at 07:30
  • @ViniVasundharan, For the view model, it just needs to be a class that contains 2 properties, one for the collection and another for the item - e.g. `public class MyModel { public IEnumerable Items { get; set; } public TCSet NewItem { get; set; } }`. Passing the ID using `ViewBag` will also work just fine but you have not shown you controller code so I can't tell what your doing wrong –  Sep 19 '15 at 07:43
  • I have added the create action method. So do you think i have to create a viewmodel to have the view that i am looking for? – Vini Sep 19 '15 at 07:50
  • @ViniVasundharan, Not necessarily (only if you want to display the collection and the form for creating a new item in the same view). But your problem is that you `Create()` method is not doing anything. You need to initialize a new instance or `TCSet`, then set its `Parent` property to the `id` value and then return the model to the view (and in the view, include a hidden input for the `Parent` property (you have not shown your model so I'm only guessing what your property names are) –  Sep 19 '15 at 07:58
  • But my create method works when it is called from the TC View. I cant call it from the the view of TCSet – Vini Sep 19 '15 at 09:52
  • I have no idea what you mean by _TC View_ and _view of TCSet_. All you have shown is `@Html.ActionLink("Create New", "Create", new {id = ViewBag.id })` which will pass the `id` value to the `Create()` view! –  Sep 19 '15 at 10:04
  • But when I execute the program the ViewBag.id doesnt pass the value. I am unable to execute that program. It exits. – Vini Sep 19 '15 at 14:40
  • TC and TCSet are the two model classes that i have working for this task. I simply meant the View for the index and create for these particular classes.. – Vini Sep 19 '15 at 14:41
  • I am little bit confused. You can have multiple actions and you can pass values between them. Try look at [this](http://stackoverflow.com/questions/10752160/send-data-between-actions-with-redirectaction-and-prg-pattern) – xxxmatko Sep 21 '15 at 06:30
  • @StephenMuecke : I get this error when i make the id in create non optional `The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Create(Int32)' in 'TCImplementation.Controllers.TCSetsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters` the value of id is passed when it is called from the TCsController but not from the createbew in TCSet. – Vini Sep 22 '15 at 05:54
  • @xxxmatko : I need to pass values beetween the action method of two different controllers. – Vini Sep 22 '15 at 06:20
  • Ok, than ViewBag is not enough. You can use ViewBag to transfer data from controller to view. You need to have the id attribute as input parameter in each of your actions, and in each view where you generate link/or form to the other action you have to pass this id - in Url (using RouteData if you are generating link) or as form field (if you are generating form which will be submitted to the other action) – xxxmatko Sep 22 '15 at 06:56
  • @xxxmatko : i have added a viewmodel to view the tc sets. now i am trying to add my create in this viewtcset. but again i need to pass the TCID which i cant do. – Vini Sep 22 '15 at 07:19

0 Answers0