ASP.NET MVC database connection using a form - asp.net-mvc

I have this form and I want it to connect to an external database. I have no idea how to and after a lot of research its still not clear to me.
Here is my code. Any guidance will be helpful.
#{
ViewData["Title"] = "Index";
}
<h2>User Information</h2>
<p>This is your user information!</p>
<form action="Result">
First name:<br>
<input type="text" name="firstname" value=" ">
<br>
Last name:<br>
<input type="text" name="lastname" value=" ">
<br>
Role:<br>
<input type="text" name="Role" value=" ">
<br>
Ranking:<br>
<input type="text" name="Ranking" value=" ">
<br><br>
<input type="submit" value="Save">
</form>
I don't really know which direction to take this form. Many thanks in advance

You don't connect the actual form itself, you connect your backend to the DB, and you connect the form to your backend.
Make some sort of route to handle the form request and then connect to your database in that route, and use it to input the data to the database.
Related links:
https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html

Your form should post data to an existing action inside an existing controller, and then, your database db connection already must be defined in your server-side MVC application.
At your point, probably, your form is looking for something called "Result"...
Now, I think that you are able to keep your research for solutions to your specific case.

Related

React Integration: How to connect react components with rails backend?

Apologies if there is a simple answer to this, but I'm very new to react.
I'm building an application in react and I'm trying to integrate two basic forms (register/login) with a rails backend. I don't really need to authenticate the user at this point, I just need it to remember the user and create a session.
Is there an easy way to do this that avoids using something like Devise? If somebody could point me in the right direction, it would be greatly appreciated.
As I mentioned the forms will be very basic. Something like this:
<form>
<label for='email'>Email Address:</label>
<input type="text" name="email" />
<label for='password'>Password:</label>
<input type="text" name="email" />
<button> Login</button>
</form>
and
<form>
<label for='email'>First Name:</label>
<input type="text" name="first-name" />
<label for='password'>Last Name:</label>
<input type="text" name="last-name" />
<label for='email'>Email Address:</label>
<input type="text" name="email" />
<label for='password'>Password:</label>
<input type="text" name="password" />
<label for='password'>Confirm Password:</label>
<input type="text" name="password" />
<button> Register</button>
</form>
Thanks in advance.
You need to create Rest Api on your Rails backend. Once you have your API endpoints you can make calls from react on those endpoints using fetch or axios whichever u like.
First, your rails backend need to be a rest API, if already is, you need to make the request of your react app, I recommend you to use Axios

MVC Display Data

Following this MVC Tutorial, but cannot get the display customer view to display after input the data. I don't see anything I do wrong
http://www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7
here is my fill customer
<div>
<form action="DisplayCustomer.aspx" method="post">
Enter Customer Code: <input type="text" name="CustomerCode" /><br />
Enter Customer Name: <input type="text" name="CustomerName" /><br />
Ennter Customer Amout: <input type="text" name="CustomerAmount" /><br />
<input type="submit" value="Submit customer data" />
</form>
</div>
here is my display customer
public ActionResult DisplayCustomer()
{
Customer localCustomer = new Customer();
localCustomer.Code = Request.Form["CustomerCode"].ToString();
localCustomer.Name = Request.Form["CustomerName"].ToString();
localCustomer.Amount = Convert.ToDouble(Request.Form["CustomerAmount"].ToString());
return View(localCustomer);
}
then
<div>
The name of the customer is: <%:Model.Name = "test" %>
<br />
The code of the customer is: <%:Model.Code %>
<br />
The amout refund to the customer is: <%:Model.Amount%>
</div>
seems like something is wrong with the steps
I got this error when I tried
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /customer/DisplayCustomer.aspx
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
The correct path for the request is /customer/displaycustomer
The .aspx extension gets handled in a different way, so the MVC Routing Engine is never fired, and as that page doesn't exist at that path the result is a 404 error.
You actually need to remove .aspx extension. Just request the page as /Customer/DisplayCustomer.
As a side note, you may need to update your form aswell and remove the aspx extension, eg:
<form action="DisplayCustomer" method="post">
Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>
replaced the line:
<form action="DisplayCustomer.aspx" method="post">
to
<form action="DisplayCustomer" method="post">

unable to display the validation messages from #Html.ValidationSummary() in MVC view

I have a fairly simple form created in a partial view and loading the form on a jquery dialog. The view is tightly bound to a model. When the user clicks on the submit button with out entering any inputs, I need to show the validation messages.
<div>
<form method="post" enctype="multipart/form-data" id="ssimForm" action="Home/ProcessUploadedFile"
onsubmit="return false;">
<div>
<h3>
Select the file to be uploaded :</h3>
<span>
<input type="file" name="UploadFileName" id="UploadFileName" /></span>
</div>
<div>
<h3>
Select the date range :</h3>
<span class="uslabel">From Date(MM/dd/yyyy): </span>
<input class="usdate" id="usfromdate" name="StartDate" type="date" />
<span class="uslabel">To Date(MM/dd/yyyy): </span>
<input class="usdate" id="ustodate" name="EndDate" type="date" />
</div>
<div>
<br />
<input type="submit" id="submitButton" name="submitButton" value="Process File" />
</div>
#Html.ValidationSummary()
<div class="message-success">
<span>#ViewBag.Confirmation</span>
</div>
<div class="message-error">
<span>#ViewBag.Error</span>
</div>
</form>
</div>
Now comes the actual problem. when I submit the form I am able to see the validationSummary object populated with the messages, but they are not getting rendered on the screen.
I am able to see the messages if I replace the content of the dialog with the response from the jquery ajax call, that fetches the entire view from the server side. I do not want to take this approach as I beleive this is not the correct way to return validation summary in MVC.
Am I missing something? Any help is appreciated.
I don't know why you don't have the #using (Html.BeginForm()) { } block.
Here is my blog post for a quick and easy way to set up Validation Summary + Unobtrusive Validation for MVC3+.
http://geekswithblogs.net/stun/archive/2011/01/28/aspnet-mvc-3-client-side-validation-summary-with-jquery-validation-unobtrusive-javascript.aspx

asp.net - show phone number as link or not

I've got a real simple ASP.NET MVC4 app that uses JQuery Mobile and displays a list of users and their information. 3 fields off the model are phone numbers. These fields can contain a null value, so I'd like to link the phone number using <a href="tel:" if it exists, and nothing if not. I came up with this:
<div data-role="fieldcontain">
<label for="textinput1"><strong>Office Phone:</strong></label>
if(!#String.IsNullOrEmpty(user.OfficePhone)){
<input name="" id="textinput1" value="#user.OfficePhone" type="text" readonly="true"/>
} else {
<input name="" id="textinput" value="#user.OfficePhone" type="text" readonly="true"/>
}
</div>
But would think there's a cleaner, better way to do this. Are there any other options or am I stuck with writing out the <input> tag twice in both conditions?
This is untested code. Hopefully it gets the idea across:
#{string pref="tel:"}
#if(String.IsNullOrEmpty(user.OfficePhone))
{pref="";
}
<div data-role="fieldcontain">
<label for="textinput1"><strong>Office Phone:</strong></label>
<input name="" id="textinput1" value="#(pref)#(user.OfficePhone)" type="text" readonly="true"/>
</div>

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

Resources