I am very much new in MVC. I am trying to show data in textbox on selection of value in dropdown.
Code for "Create View":
#model CRUD_using_LinQ_to_SQL_in_MVC.Models.OtdModelClass
#{
ViewBag.Title = "Create";}
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Create</legend>
<div class="editor-label" style="font-weight: bold">
Server Type:
<//div>
<div>
<select name="Server Type" id="ServerType">
<option>Select</option>
<option value="1">Internet</option>
<option value="2">Lease</option>
</select>
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.MemberCode)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.MemberCode, new { maxlength="6"})
#Html.ValidationMessageFor(model => model.MemberCode)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.LoginID)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.LoginID, new { maxlength = "6" })
#Html.ValidationMessageFor(model => model.LoginID)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.OTDPassword)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.OTDPassword, new { maxlength = "7" })
#Html.ValidationMessageFor(model => model.OTDPassword)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.BBSID)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.BBSID, new { maxlength = "6" })
#Html.ValidationMessageFor(model => model.BBSID)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.IPAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.IPAddress)
#Html.ValidationMessageFor(model => model.IPAddress)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.ServerType)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ServerType)
#Html.ValidationMessageFor(model => model.ServerType)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.OTDStatus)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OTDStatus)
#Html.ValidationMessageFor(model => model.OTDStatus)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.RemoteIP)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.RemoteIP)
#Html.ValidationMessageFor(model => model.RemoteIP)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.RemotePort)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.RemotePort)
#Html.ValidationMessageFor(model => model.RemotePort)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.AllowDownload)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.AllowDownload)
#Html.ValidationMessageFor(model => model.AllowDownload)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.OTDTimeStamp)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.OTDTimeStamp)
#Html.ValidationMessageFor(model => model.OTDTimeStamp)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.MemberType)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MemberType)
#Html.ValidationMessageFor(model => model.MemberType)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.EQ)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EQ)
#Html.ValidationMessageFor(model => model.EQ)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.EQD)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EQD)
#Html.ValidationMessageFor(model => model.EQD)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.BFX)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.BFX)
#Html.ValidationMessageFor(model => model.BFX)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.SLB)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.SLB)
#Html.ValidationMessageFor(model => model.SLB)
</div>
<div class="editor-label" style="font-weight: bold">
#Html.LabelFor(model => model.Others)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Others)
#Html.ValidationMessageFor(model => model.Others)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>}<div> #Html.ActionLink("Back to List", "Index")</div>
Here when user select option "Internet" in server type then "Remote IP" and "Remote Port" fields should get autofilled with respective values.
I searched a lot on internet but i didnt get proper solution for this.
Can you please help me?
Thanks in advance...
That's because you're coding the drop down yourself in HTML. What you should be using is the DropDownListFor method in MVC.
This answer can provide you with an example.
Please try the below,
1. Create a jquery onchange event for the dropdown you mentioned.
2. Get the selected value.
3. Create a new action method with Json return type and input parameter as string named ServerType. And query the database to get the RemoteIp and Remoteport values based on the server type.
4. In the jquery onchange event do a ajax call to the action method created in step 3.
5. Form the json data as "{"ServerType": "Your selected dropdown value"}.
6. In the ajax success method assign the text box with the respective data from the success method result.
Hope you know using jquery better using ajax call. You may use the Html.Dropdownfor() control as the other answer suggests. Please let me know if any information required.
Related
It is a create view page where i have insert supplier information and it is inserting data successfully. i want to get this data automatically when i will enter a name and it exist it will show all of the data in these field, if not exist then i will save to the table.
#model FCBook.Supplier
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Supplier</legend>
<div class="editor-label">
#Html.LabelFor(model => model.SupplierName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.SupplierName)
#Html.ValidationMessageFor(model => model.SupplierName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PhoneNo)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.PhoneNo)
#Html.ValidationMessageFor(model => model.PhoneNo)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Area)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Area)
#Html.ValidationMessageFor(model => model.Area)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.District)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.District)
#Html.ValidationMessageFor(model => model.District)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Division)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Division)
#Html.ValidationMessageFor(model => model.Division)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Country)
#Html.ValidationMessageFor(model => model.Country)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
My controller action to this create view is
public ActionResult Create()
{
return View();
}
//
// POST: /Supplier/Create
[HttpPost]
public ActionResult Create(Supplier collection)
{
try
{
// TODO: Add insert logic here
var db = new PetaPoco.Database("FCBook");
if (collection != null)
{
collection.Insert();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I think you want to look into using Remote validation. It can be triggered after a user enters a value into one of your fields. I'm using this approach to populate other fields upon such an event occurring.
I'm really having issues with the lambda expressions.
#model CalculateSimpleInterest.Models.SimpleInterestModel
#{
ViewBag.Title = "SimpleInterest";
}
<h2>Calulate Simple Interest</h2>#using (Ajax.BeginForm("CalculateSimpleInterestResult","CalculateSimpleInterest",
new AjaxOptions { UpdateTargetId = "divInterestDeatils" }))
{
<fieldset>
<legend>Calulate Simple Interest</legend><div id="div1"></div>
<div class="editor-label">
#Html.LabelFor(model => model.Amount)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Amount)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Year)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Year)
</div>
<p>
<input type="submit" value="Calculate" />
</p>
</fieldset>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
The syntax you are looking for is
#Html.YourHelperFor(Function(model), model.Property)
eg
<div class="editor-label">
#Html.LabelFor(Function(model), model.Amount)
</div>
I have form like
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Course</legend>
#Html.HiddenFor(model => model.CId)
<div class="editor-label">
#Html.LabelFor(model => model.CName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CName)
#Html.ValidationMessageFor(model => model.CName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Cteator)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Cteator)
#Html.ValidationMessageFor(model => model.Cteator)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.date)
#Html.ValidationMessageFor(model => model.date)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
I want to insert system current date and time in the field of Date field. How can i do this. Model and cotroller are already create by scaffolding.
Insert this before you call the editor (assuming your property is a DateTime)
#{ Model.date = DateTime.Now; }
If your property is a string, call DateTime.Now.ToString().
You could set the default value in the corresponding controller action:
public ActionResult Index() {
return View(new SomeModel { date = DateTime.Now });
}
Change your code as mention below
#{ Model.date = DateTime.Now.ToString(); }
#Html.ValidationSummary(true)
<fieldset>
<legend>Course</legend>
#Html.HiddenFor(model => model.CId)
<div class="editor-label">
#Html.LabelFor(model => model.CName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CName)
#Html.ValidationMessageFor(model => model.CName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Cteator)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Cteator)
#Html.ValidationMessageFor(model => model.Cteator)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.date)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.date)
#Html.ValidationMessageFor(model => model.date)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
I have created one View. In that i have put 2 Multisubmit buttons for "create" and "cancel".
Now, on cancel click, clientside validation is going to call. but I don't want to call clientside validations.
I have written code as below, please check it. I don't want validations to be executed on cancel button click.
#model Blog.Models.User
#{
ViewBag.Title = "Register New User";
}
#using Microsoft.Web.Mvc;
#using MVC3MultiSubmitButtonExtension;
<h2>
Register New User</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm())
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Fill User Details</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.EmailAddress)
#Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MobileNumber)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MobileNumber)
#Html.ValidationMessageFor(model => model.MobileNumber)
</div>
<p>
#* <input type="submit" value="Create" />*#
#Html.MultiSubmitButton(Url.Action("Create"), "btnCreate", "Create")
#Html.MultiSubmitButton(Url.Action("Cancel"), "btnCancel", "Cancel")
</p>
</fieldset>
}
<div>
#Html.ActionLink("Click here to go Login Page", "Index")
</div>
If you assign class="cancel" to your cancel submit button, clientside validation won't run. The way you assign this class is not something that I can tell as you seem to be using some custom helper Html.MultiSubmitButton which is not part of ASP.NET MVC. So you will have to check its documentation.
I am working on a hotel Reservation Project in MVC 3
Here I display the rooms:
When i click to reserve a romm i get here :
But what i want to do is to get the number of the room i selected directly in the Reservation form room box.
Here is my Create view for reservation:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Reservation</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Room_ID, "Room")
</div>
<div class="editor-field">
#Html.DropDownList("Room_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Room_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Guest_ID, "Client")
</div>
<div class="editor-field">
#Html.DropDownList("Guest_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Guest_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Data_Check_in)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Data_Check_in)
#Html.ValidationMessageFor(model => model.Data_Check_in)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Data_Check_out)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Data_Check_out)
#Html.ValidationMessageFor(model => model.Data_Check_out)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Preference_ID, "Preferinte")
</div>
<div class="editor-field">
#Html.DropDownList("Preference_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Preference_ID)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Price)
#Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Card_ID, "Card_de_credit")
</div>
<div class="editor-field">
#Html.DropDownList("Card_ID", String.Empty)
#Html.ValidationMessageFor(model => model.Card_ID)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Can you pass the room id from the first page to the form page using a view model?