Xaeed Butt Xaeed Butt - 3 months ago 15
C# Question

getting data from unbinded view and submitting form data with linq into tables

I have a form view which is not bound to any action or class. I want to get data of that form and insert into my tables through action using

LINQ


@Html.BeginForm("New", "User", "GET")
{
<div class="form-group">
@Html.Label("Name", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Editor("Name", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessage("Name", "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Email", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Editor("Email", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessage("Email", "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}


Controller "new "



[HttpPost]
public ActionResult New(FormCollection _col)
{
if (ModelState.IsValid)
{
string name =_col["Name"].ToString();

//db.tbl_User.Add(_col);
db.SaveChanges();
return RedirectToAction("Index");
}

return View();
}

Answer

HTML

@using (Html.BeginForm("New", "User", FormMethod.Post))
{

@Html.ValidationSummary(true, "You may encounter some error as shown below:")

<div class="form-group">
    @Html.Label("Name", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.Editor("Name", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessage("Name", "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    @Html.Label("Email", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.Editor("Email", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessage("Email", "", new { @class = "text-danger" })
    </div>
</div>
<div class="form-group">
    <div class="col-md-offset-2 col-md-10">
        <input type="submit" value="Create" class="btn btn-default" />
    </div>
</div>
}

CS

    [HttpPost]
    public ActionResult New(FormCollection _col)
    {
        using (DBContext db = new DBContext())
        {
            ValidateFormCollection(_col);

            if (ModelState.IsValid)
            {
                User user = new User();
                user.Name = _col["Name"].ToString();
                user.Email = _col["Email"].ToString();

                db.Users.Add(user);
                db.SaveChanges();
                return View("Index");
            }
            else
            {
                return View();
            }
        }
    }

    private void ValidateFormCollection(FormCollection _col)
    {
        if (string.IsNullOrEmpty(_col["Name"].ToString()))
            ModelState.AddModelError("Name", "The Name field is required.");

        if (string.IsNullOrEmpty(_col["Email"].ToString()))
            ModelState.AddModelError("Email", "The Email field is required.");
    }