ASP MVC3 how to add system current date? - asp.net-mvc

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>

Related

How auto retrieve data from database in mvc?

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.

What is the VB.NET equivalence of this razor syntax?

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>

MultiSubmitButton Clientside validation

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.

get the room by id

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?

How redirected user with method POST?

How to redirect a user from the action with POST request?
I have:
public ViewResult AddMoneyOnPay()
{
Onpay onpay = new Onpay();
onpay.convert = "yes";
onpay.pay_mode = "fix";
onpay.price_final = "true";
return View(onpay);
}
View:
#using (Html.BeginForm("AddMoneyOnPay", "Transaction", FormMethod.Post))
{
#Html.ValidationSummary(true)
<fieldset>
<legend>Onpay</legend>
<div class="editor-field">
#Html.EditorFor(model => model.pay_mode)
#Html.ValidationMessageFor(model => model.pay_mode)
</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-field">
#Html.EditorFor(model => model.currency)
#Html.ValidationMessageFor(model => model.currency)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.pay_for)
#Html.ValidationMessageFor(model => model.pay_for)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.md5)
#Html.ValidationMessageFor(model => model.md5)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.convert)
#Html.ValidationMessageFor(model => model.convert)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.url_success)
#Html.ValidationMessageFor(model => model.url_success)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.url_fail)
#Html.ValidationMessageFor(model => model.url_fail)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user_email)
#Html.ValidationMessageFor(model => model.user_email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.note)
#Html.ValidationMessageFor(model => model.note)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.one_way)
#Html.ValidationMessageFor(model => model.one_way)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.price_final)
#Html.ValidationMessageFor(model => model.price_final)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.direct_no)
#Html.ValidationMessageFor(model => model.direct_no)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
action POST
[HttpPost]
public ViewResult AddMoneyOnPay(Onpay onpay)
{
string secretKEy = "fH12312IO";
onpay.md5 = GetHash(string.Format("{0};{1};{2};{3};{4}", onpay.pay_mode, onpay.price, onpay.currency, onpay.pay_for, onpay.convert, secretKEy));
//SEND POST to url http://paysite.ru/blablabla
}
Have your AddMoneyOnPay method return an ActionResult and then write:
return Redirect(returnUrl);

Resources