Reputation: 303
Here I have am creating a small application. Where I have this HTML page where I enter the data. On clicking Submit button the data should get stored in the database through a stored procedure. I have written insert statement in stored procedure.
html code:
@model USTGlobal.WorkBench.UI.Models.FloorPlanViewModel
@{
ViewBag.Title = "RequestForm";
}
<div>
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Request Form</h2>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-4">Period:</label>
<div class="col-lg-8">
@Html.DropDownList("Quarter", new SelectListItem[] { (new SelectListItem() { Text = "Q1", Value = "1"}), (new SelectListItem() { Text = "Q2", Value = "2" }), (new SelectListItem() { Text = "Q3", Value = "3" }), (new SelectListItem() { Text = "Q4", Value = "4"}) }, "-- Select Quarter --", new { @class = "form-control" })
<br />
@Html.DropDownList("Year", new SelectListItem[] { (new SelectListItem() { Text = "2016", Value = "2016" }), (new SelectListItem() { Text = "2017", Value = "2017" }) }, "-- Select Year --", new { @class = "form-control" })
</div>
</div>
</div>
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-4">Line ID:</label>
<div class="col-lg-8">
@Html.TextBoxFor(model => model.floorConfig.LineID, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
</div>
</div>
</div>
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-4">Project:</label>
<div class="col-lg-8">
@Html.TextBoxFor(model => model.floorConfig.Project, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
</div>
</div>
</div>
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-lg-4">Budget:</label>
<div class="col-lg-8">
@Html.TextBoxFor(model => model.floorConfig.Budget, new { onkeypress = "return isNumberKey(event)", @class = "form-control" })
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-offset-4" style="padding: 10px 0px 0px 0px;">
<input type="submit" id="btnSubmit" value="Submit" class="btn btn-lg btn-success" />
<input type="button" id="btnCancel" value="Clear" class="btn btn-lg btn-success" />
</div>
</div>
</div>
FloorPlanviewmodel: public FloorConfirguration floorConfig { get; set; }
Repository code:
public class FloorConfirguration
{
public string Quarter { get; set; }
public int Year { get; set; }
public string LineID { get; set; }
public string Project { get; set; }
public decimal Budget { get; set; }
}
How to move forward from here? Should I make the call to the stored procedure in controller
?
Upvotes: 0
Views: 45
Reputation: 142
You can use
View;
@using (Html.BeginForm("ActionName", "Controller", FormMethod.Post))
{
//Add your desing
}
Controller;
[HttpPost]
public ActionResult ActionName(Model model)
{
if (ModelState.IsValid)
{
try
{
//Create SQL class and write your method (insert, update or delete etc.).
bool check = SQLManager.Check(model);
if (check)
{
//Make something
}
else
{
//Return error
return View("Index");
}
}
catch (Exception ex)
{
//Return error
return View("Index");
}
}
else
{
//Return error
return View("Index");
}
}
Upvotes: 0
Reputation: 8255
You can tell Entity Framework to execute a particular SQL Command using DbContext.Database.ExecuteSqlCommand
This method will execute any command you specify, whether its a string containing SQL code or the name of a Stored Procedure.
ExecuteSqlCommand
accepts an array for parameters as well so to execute Stored Procedure that takes parameter values (as yours should) for each new bit of data you want to insert, you could try this:
List<SqlParameter> args = new List<SqlParameter>();
args.Add(new SqlParameter("@parameterName", parameterValue));
// Keep adding parameters like this
using (MyDatabaseContext db = new MyDatabaseContext())
{
db.Database.ExecuteSqlCommand("StoredProcedureName", args.ToArray());
}
This will be execute in the ActionResult function that your submit button posts to in your Controller.
Upvotes: 1