0

I'm inserting data from one (model)table to another, but it is passing null values.

Here is the code for the controller :

 public ActionResult PendingBlogs()
    {
        OMSDataContext db = new OMSDataContext();

        var query = from a in db.BlogApprovals
                    select a;
        return View(query);
    }
    [HttpPost]
    public ActionResult PendingBlogs(BlogApproval blogap)
    {
        OMSDataContext db = new OMSDataContext();

        Blog b = new Blog
        {
            BlogTitle = blogap.BlogTitle1,
            BlogContent = blogap.BlogContent1,
            UserName = blogap.UserName1,
            Date = blogap.Date1,
            IsApproved = true
        };
        db.Blogs.InsertOnSubmit(b);
        db.SubmitChanges();

        return RedirectToAction("Index");
    }

And here is my View code :

@model IEnumerable<MVCDemo.Models.BlogApproval>
@{
ViewBag.Title = "PendingBlogs";
}

<h2>PendingBlogs</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.BlogTitle1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.BlogContent1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserName1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsApproved1)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.BlogTitle1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BlogContent1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserName1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.IsApproved1)
        </td>
        <td>
            <form asp-controller="Admin" method="post">
                <input type="submit" name="answer" value="RunRegisterdJob" />
            </form>
        </td>
    </tr>
}

</table>

The two models (Blog and BlogApproval) are tables in the models folder. BlogApproval has data inside it, but it's not returning the data from the table for some reason.

I've looked at this thread, and have renamed the columns in the BlogApproval table so that they are different from the columns on the Blog table, but that didn't fix it.

  • You form has nor form controls - there is nothing to send in the request to the controller (except the value of the submit button)! –  Jan 19 '18 at 08:04
  • OK, that was my bad. So I have to apply form controls to all the data that is sent back to the controller? Should I just make a new action method with a different view ? – Jared Padeleki Jan 19 '18 at 08:08
  • `@Html.DisplayFor` not create any form controls - it create text placeholders instead (as `div` or `span` element). If you want to use form control with POST request, use `@Html.TextBoxFor`, `@Html.HiddenFor` or any helper which can produce form controls. – Tetsuya Yamamoto Jan 19 '18 at 08:09
  • Yes, But it seems you only want to update the `IsApproved` property anyway so all you should be doing is passing back the `ID` of the record (as a route value in the `
    ` or a hidden input inside the form, and then in the POST method, get the record from the db based on the `ID` and update it.
    –  Jan 19 '18 at 08:13
  • @StephenMuecke sorry, but how should I go about returning the ID back? Where should I place the @Html.HiddenFor(m => item.Id) ? – Jared Padeleki Jan 19 '18 at 08:46
  • You could just use `
    ` and change the POST method to `public ActionResult PendingBlogs(int Id)`
    –  Jan 19 '18 at 08:50
  • @StephenMuecke I did what you said, and I am getting a 'System.ArgumentException' parameter 'Id' of non-nullable type 'System.Int32' – Jared Padeleki Jan 19 '18 at 09:51
  • Then its due to code you have not shown me :). But you can always add a hidden input inside your `
    ` as an alternative - i.e. just `` (don't use a tag helper for it)
    –  Jan 19 '18 at 09:54

1 Answers1

0

Your form tag does not contain any html input value ,so instead of using below code

<form asp-controller="Admin" method="post">
        <input type="submit" name="answer" value="RunRegisterdJob" />
</form>

use below code:

<form asp-controller="Admin" method="post">
       <input type="text" name="blogTitle1"/>
       <input type="text" name="blogContent"/>
       <input type="text" name="UserName1"/>
       <input type="submit" name="answer" value="RunRegisterdJob" />
</form>