I am new to MVC and need help to upload File , I am using umbraco. 7.2.1
I am trying to send mail with mail with attachment
Following is my code for the same.
Partial View ==>name Contact
using (Html.BeginUmbracoForm<ContactVController>("HandleContactSubmit"))
{
#Html.LabelFor(model => model.Name)<br />
#Html.EditorFor(model => model.Name)<br />
#Html.ValidationMessageFor(model => model.Name)<br />
#Html.LabelFor(model => model.Email)<br />
#Html.EditorFor(model => model.Email)<br />
#Html.ValidationMessageFor(model => model.Email)<br />
<input type="file" name="file" id="file" />
<p>
<input type="submit" value="Submit" />
</p>
}
Model
public class ContactVModel
{
[Required]
public string Name { get; set; }
[Required]
[RegularExpression(#"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]
public string Email { get; set; }
[Required]
public string Message { get; set; }
public HttpPostedFileBase attachment { get; set; }
}
Controller
public class ContactVController : SurfaceController
{
[HttpPost]
public ActionResult HandleContactSubmit(ContactVModel model)
{
,.......... ...... ....
,.......... ...... ....
MailBody + = model.Name ;
MailBody + = model.Email;
SendMail( MailBody )
}
But I do not know access model.attachment , How can I do so to send mail with attachment (the file which is uploaded ) (As I am able to acces Name, Email, etc.)
I have referred following post but I could not able to access attachment
MVC 4 Razor File Upload
but I could not make it out
Change the input from
<input type="file" name="file" id="file" />
to
<input type="file" name="attachment" id="attachment" />
So the property in the model matches the input field.
I'm currently using the following:
In the cshtml file:
#Html.UploadFor(model=> model.Attachment)
static public class HtmlExtensions
{
public static IHtmlString UploadFor<TSource, TResult>(this HtmlHelper<TSource> html, Expression<Func<TSource, TResult>> propertyExpression)
{
var memberName = Reflection.GetPropertyName(propertyExpression);
return new HtmlString($"<input type=\"file\" name=\"{memberName}\" id=\"{memberName}\">");
}
}
class Reflection
{
public static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> propertyExpression)
{
return (propertyExpression.Body as MemberExpression).Member.Name;
}
}
Related
I have this ViewModels:
Inside "EMPRESA" I got a lot of properties that work just fine.
public class EmprViewModel
{
public int ID_EMP { get; set; }
public string NOME { get; set; }
public List<TelasViewModel> Telas;
}
public class TelasViewModel
{
public int ID_TELA { get; set; }
public string TITULO { get; set; }
public bool Valor { get; set; }
public string Selecionado { get; set; }
}
My Razor is:
#model SYSTEMNAME.Models.EmprViewModel
<div class="container espaco">
#using (Html.BeginForm("SalvarAlteracao", "Empresas", FormMethod.Post, new { #id = "frmEmpresas" }))
{
#Html.HiddenFor(model => model.ID_EMP)
<div class="row">
#Html.EditorFor(m => Model.Telas)
</div>
}
And I created in ~\Shared\EditorTemplates\TelasViewModel.cshtml
#model SYSTEMNAME.Models.TelasViewModel
#Html.HiddenFor(m => m.ID_TELA)
#Html.HiddenFor(m => m.TITULO)
#Html.HiddenFor(m => m.Selecionado)
<div class="cols3-25"><label>#Model.TITULO: </label></div>
<div class="cols3-75">#Html.CheckBoxFor(m => m.Valor)</div>
The output code generated in Razor is fine, for each "Telas" Object I got something like this:
<input data-val="true" data-val-number="O campo ID_TELA deve ser um número." data-val-required="O campo ID_TELA é obrigatório." id="Telas_0__ID_TELA" name="Telas[0].ID_TELA" type="hidden" value="2">
<input id="Telas_0__TITULO" name="Telas[0].TITULO" type="hidden" value="Contatos">
<input id="Telas_0__Selecionado" name="Telas[0].Selecionado" type="hidden" value="">
<div class="cols3-25"><label>Contatos: </label></div>
<div class="cols3-75"><input data-val="true" data-val-required="O campo Valor é obrigatório." id="Telas_0__Valor" name="Telas[0].Valor" type="checkbox" value="true"><input name="Telas[0].Valor" type="hidden" value="false"></div>
But When I get the FormPost, the list comes "null"
[HttpPost]
public async Task<ActionResult> SalvarAlteracao(EmpresaViewModel EmpresaAlterado)
{
...
// Here if I try anything with EmpresaAlterado.Telas, it say "Telas is null";
}
What I'm maybe doing wrong or forgeting in this code?
Edit:
I the Post, there It's posting twice the field "Valor" like:
... &Telas%5B0%5D.ID_TELA=2&Telas%5B0%5D.TITULO=Contatos&Telas%5B0%5D.Selecionado=selected&Telas%5B0%5D.Valor=true&Telas%5B0%5D.Valor=false&Telas%5B1%5D.ID_TELA=3&Telas%5B1%5D.TITULO=Usuarios&Telas%5B1%5D.Selecionado=selected&Telas%5B1%5D.Valor=true&Telas%5B1%5D.Valor=false& ...
Can it have something to do With the two fields that "CheckboxFor" is creating on the Html? Here the example:
<div class="cols3-75"><input data-val="true" data-val-required="O campo Valor é obrigatório." id="Telas_0__Valor" name="Telas[0].Valor" type="checkbox" value="true"><input name="Telas[0].Valor" type="hidden" value="false"></div>
Only public properties are filled, check out that Telas it's a public variable, not property
Changing to
public List<TelasViewModel> Telas { get; set; }
will fix the issue
I have problems getting my blog create comments working it works fine when I edit in the db putting the relation id in but I cannot get it when I use the form.
Error is here: Blogposts = id
public ActionResult BlogPost(int Id)
{
var _getSpecificBlogPost = db.Blogposts.Where(m => m.Id == Id).ToList();
return View(_getSpecificBlogPost);
}
[HttpPost]
public ActionResult BlogComment_Create(string name, string bodytext, string id )
{
BlogComment model = new BlogComment { Name = name, BodyText = bodytext, Blogposts = id};
db.BlogComments.Add(model);
db.SaveChanges();
return Redirect(Request.UrlReferrer.PathAndQuery);
}
In BlogComment I have: public virtual Blogpost Blogposts { get; set; }
In BlogPosts I have: public virtual ICollection<BlogComment> BlogComments { get; set; }
<form method="post" id="form-variant-create" action="#Url.Action("BlogComment_Create", "Blog")" role="form">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" />
<br />
<textarea name="bodytext" class="form-control" rows="3" placeholder="Message"></textarea>
<input type="text" class="hidden" name="id" value="#foreach(var item in Model){#item.Id}" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
BlogPosts is from BlogPost type. you need to create a property for your BlogPostId as ForeignKey.
public virtual int BlogPostId{get;set;}
Insert this line into your CommentModel. and then reffer to this instead of BlogPosts.
and edit your BlogPosts Property too.
[ForeignKey("BlogPostId")]
public virtual Blogpost Blogposts { get; set; }
I have a view with the name "Create". This view gets the "SchoolViewModel" which contains two classes:
public class SchoolViewModel
{
public List<Teacher> ListTeacher { get; set; }
public List<SchoolClass> ListSchoolClass { get; set; }
public ClassComplete ClassComplete { get; set; }
}
Each list in "SchoolViewModel" provides data from a database.
At the "Create" page you should be able now to select a teacher and class (DropDownList). The "ClassComplete" object contains the two classes (Teacher and SchoolClass) and the roomname
public class ClassComplete
{
public string RoomName { get; set; }
public SchoolClass SchoolClass { get; set; }
public Teacher Teacher { get; set; }
}
I want only to post the "ClassComplete" object.
My ActionResult
[HttpPost]
public ActionResult Create(ClassComplete cp)
{
// Do something
return View();
}
Edit:
Razor View
#using (Html.BeginForm())
{
#Html.EditorFor(m => m.ListTeacher[0].TeacherName)
#Html.EditorFor(m => m.ListSchoolClass[0].ClassName)
#Html.TextBoxFor(m => m.cl.RoomName)<br />
<input type="submit" value="Click" />
}
Is this the right way ?
best regards
If you want to POST only ClassComplete model you will need to indicate the binding prefix:
[HttpPost]
public ActionResult Create([Bind(Prefix="ClassComplete")] ClassComplete cp)
{
// Do something
return View();
}
and in your view:
#using (Html.BeginForm())
{
#Html.TextBoxFor(m => m.ClassComplete.RoomName)
<br />
<input type="submit" value="Click" />
}
The TextBoxFor will generate the following input field in the resulting markup:
<input type="text" name="ClassComplete.RoomName" />
Notice the name of the input field. That's the reason why you need to indicate this prefix in your controller action.
This will also work for the other properties if you want to send them you just need to include the corresponding input fields:
#Html.TextBoxFor(m => m.ClassComplete.SchoolClass.SomeProperty)
#Html.TextBoxFor(m => m.ClassComplete.Teacher.SomeOtherProperty)
...
Why is the value of my checkbox not passed to my ViewModel?
My View (I omitted input tags not relevant for this post):
#model Pro.WebUI.ViewModels.UserViewModel
#using (Html.BeginForm("ManageUsers", "Administration", FormMethod.Post,
new { id = "request-form", #class = "form-horizontal" }))
{
<div class="form-group">
<label for="inputAuthorize" class="col-lg-2 control-label">Authorize</label>
<div class="col-lg-8">
<input type="checkbox" id="Authorized" name="Authorized" value="#Model.Authorized" />
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<br /><br />
<button type="submit" class="btn btn-primary">Submit Request</button>
</div>
</div>
}
My ViewModel:
public class UserViewModel
{
[Key]
public string UserID { get; private set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool Authorized { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Notes { get; set; }
}
My Controller:
[HttpPost]
public ActionResult ManageUsers(UserViewModel model)
{
if (ModelState.IsValid)
{
ProcurementUser obj = new ProcurementUser();
obj.UserName = model.Email;
obj.FirstName = model.FirstName;
obj.LastName = model.LastName;
obj.Email = model.Email;
obj.Phone = model.Phone;
obj.Authorized = model.Authorized;
UserRepository.SaveUser(obj);
//success message
}
return View(model);
}
I did not include all input tags but when I step through the code without the checkbox, all values are passed. I looked at other checkbox questions on SOF but they mostly use the #Html.Checkbox or #Html.CheckboxFor. I would like to just use input type="checkbox"
If we need to use <input> filed instead of #Html.CheckboxFor, we can use "checked=\"checked\"" syntax as in this code:
<input type="checkbox" id="Authorized" name="Authorized" value="true" #(Model.Authorized ? "checked=\"checked\"" : "") />
As has been hinted at in the comments the issue you're having is that you're not really creating your checkbox correctly:
Assuming your model has Authorized = true your mark-up would be:
<input type="checkbox" id="Authorized" name="Authorized" value="true" />
Similarly the false state would result in:
<input type="checkbox" id="Authorized" name="Authorized" value="false" />
But these aren't "checked" checkboxes - they're still "unchecked", and need the checked attribute setting:
<input type="checkbox" id="Authorized" name="Authorized" value="true" checked />
As Stephen points out - an unchecked checkbox will not send any data back to the server so that you don't get confused about which options where selected.
Finally, as has also been noted, your <label> element is for an non-existent field looking for inputAuthorize instead of Authorized.
All of these issues would be taken care of for you if you were to use the #Html.CheckboxFor and #Html.LabelFor helper classes.
I have a view with a number of checkboxes in it. I want to be able to pass the values of the checkboxes to the controller, then output a list of the OfficeNames that have been ticked. I am not sure how to pass the values of multiple checkboxes back to the controller, or how to output the OfficeNames based on which boxes have been ticked
View:
<p>
#using (Html.BeginForm())
{
<p>
Start Date: #Html.TextBox("StartDate") <br />
<br />
End Date: #Html.TextBox("EndDate") <br />
<br />
<input type="submit" value="Filter" />
</p>
}
<p>
#foreach (var item in Model.BettingOffices)
{
<label>#Html.DisplayFor(modelItem => item.OfficeName)</label>
<input type="checkbox" name="selectedShops" value="#item.OfficeName">
}
</p>
Controller:
public class DailyReportController : Controller
{
private RiskEntities _db = new RiskEntities();
// GET: /DailyReport/
public ActionResult Index(DateTime? startDate, DateTime? endDate)
{
if (startDate == null || endDate == null)
{
var dailyReportModelBlank = new DailyReportModel();
dailyReportModelBlank.BettingOffices = (from bo in _db.BettingOffices orderby bo.OfficeName select bo ).ToList();
//dailyReportModelBlank.DailyReports.Add(new DailyReport());
return View(dailyReportModelBlank);
}
var endDateToUse = (DateTime) endDate;
endDateToUse = endDateToUse.AddDays(+1);
var dailyReportModel = new DailyReportModel
{
DailyReports = (from dr in _db.DailyReports
where dr.DailyReportDate >= startDate
&& dr.DailyReportDate <= endDateToUse
select dr).ToList(),
BettingOffices = (from bo in _db.BettingOffices select bo).ToList()
};
return View(dailyReportModel);
}
Model:
public class DailyReportModel
{
private List<DailyReport> _dailyReports = new List<DailyReport>();
private List<BettingOffice> _bettingOffices = new List<BettingOffice>();
public List<DailyReport> DailyReports
{
get { return _dailyReports; }
set { _dailyReports = value; }
}
public List<BettingOffice> BettingOffices
{
get { return _bettingOffices; }
set { _bettingOffices = value; }
}
}
BettingOffice Class:
public partial class BettingOffice
{
public int BettingOfficeID { get; set; }
public string OfficeName { get; set; }
public string OfficeCode { get; set; }
public string IpAddress { get; set; }
public Nullable<bool> SupportOnly { get; set; }
public Nullable<int> SisSrNumer { get; set; }
public Nullable<bool> Local { get; set; }
public string Server { get; set; }
}
try this :
<p>
#using (Html.BeginForm())
{
<p>
Start Date: #Html.TextBox("StartDate")
<br />
<br />
End Date: #Html.TextBox("EndDate")
<br />
<br />
<input type="submit" value="Filter" />
</p>
}
</p>
<p>
#foreach (var item in Model.BettingOffices)
{
<label>#Html.DisplayFor(modelItem => item.OfficeName)</label>
<input type="checkbox" name="bettingOfficeIDs" value="#item.BettingOfficeID">
}
</p>
And in your Action you can get the selected office ids in bettingOfficeIDs variable:
public ActionResult YourActionName(int[] bettingOfficeIDs)
Few things that need to change here.
If you want values to be passed to action method they need to be within form not outside
For MVT to 'understand' checkbox values as array (or more complex object) you need to work with their html name attribute.
I will do demonstration application below that should help you understand how it works:
CsHtml: Notice that you need to add value attribute to checkboxes to be able to read their values, checkbox gets true only when checkbox is ticked and value is true, hence the javascript. You can add as many of complex object properties as hidden fields as long as you give them names that match to the object property names in viewModel. In this case I am only passing BettingOfficeID
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(document).on("click", "[type='checkbox']", function(e) {
if (this.checked) {
$(this).attr("value", "true");
} else {
$(this).attr("value","false");}
});
<p>
#using (Html.BeginForm())
{
<p>
Start Date: #Html.TextBox("StartDate") <br />
<br />
End Date: #Html.TextBox("EndDate") <br />
<br />
</p>
<p>
<input type="checkbox" name="BettingOffices[0].Selected" value="true">
<input type="hidden" name="BettingOffices[0].BettingOfficeID" value="1">
<input type="checkbox" name="BettingOffices[1].Selected" value="false">
<input type="hidden" name="BettingOffices[1].BettingOfficeID" value="2">
<input type="checkbox" name="BettingOffices[2].Selected" value="true">
<input type="hidden" name="BettingOffices[2].BettingOfficeID" value="3">
<input type="checkbox" name="BettingOffices[3].Selected" value="false">
<input type="hidden" name="BettingOffices[3].BettingOfficeID" value="4">
<input type="checkbox" name="BettingOffices[4].Selected" value="true">
<input type="hidden" name="BettingOffices[4].BettingOfficeID" value="5">
</p>
<input type="submit" value="submit"/>
}
Post Action method to add to controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(BettingViewModel viewModel)
{
return null;
}
BettingViewModel: I have added Selected property to BettingOffice class.
public class BettingViewModel
{
public string StartDate { get; set; }
public string EndDate { get; set; }
public List<BettingOffice> BettingOffices { get; set; }
}
public class BettingOffice
{
public bool Selected { get; set; }
public int BettingOfficeID { get; set; }
public string OfficeName { get; set; }
public string OfficeCode { get; set; }
public string IpAddress { get; set; }
public Nullable<bool> SupportOnly { get; set; }
public Nullable<int> SisSrNumer { get; set; }
public Nullable<bool> Local { get; set; }
public string Server { get; set; }
}
Hope this saves you some time.
View:
#using (Html.BeginForm("Createuser", "User", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<div class="form-group">
#Html.LabelFor(m => m.city, new { #class = "col-md-2 control-label" })
</div>
<div class="col-md-10">
<table>
<tr>
<td><input type="checkbox" name="city" value="Pune" id="1" />Pune</td>
<td><input type="checkbox" name="city" value="Banglore" id="2" />Banglore</td>
<td><input type="checkbox" name="city" value="Mumbai" id="3" />Mumbai</td>
</tr>
</table>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Create" />
</div>
</div>
}
[HttpPost]
public ActionResult Createuser(user user, string [] city)
{
var UserInfo = new user
{ Email =user.Email,Password=user.Password,Firstname=user.Firstname };
return View();
}
1. First of all, you are generating checkboxes with same name. So how you will be able to retrieve them on server end separately?
So declare some counter that gets incremented and name checkboxes uniquely.
#foreach (var item in Model.BettingOffices)
{
int counter=1;
var checkboxName = "selectedShops" + counter;
<label>#Html.DisplayFor(modelItem => item.OfficeName)</label>
<input type="checkbox" name="#checkboxName" value="#item.OfficeName">
counter++;
}
2. Now on submission of Form in your controller, get checkboxes as -
//Loop through the request.forms
for (var i = 0; i <= Request.Form.Count; i++)
{
var checkboxValue = Request.Form["selectedShops[" + i + "]"];
// Do whatever you want to with this checkbox value
}
For ticked values, you will probably get True value. Debug the retrieved value to write further code accordingly.
Try the following
your View is:
#foreach (var item in Model.BettingOffices)
{
<label>#Html.DisplayFor(modelItem => item.OfficeName)</label>
<input type="checkbox" name="selectedShops" value="#item.OfficeName">
}
Controller
[HttpPost]
public ActionResult Index(FormCollection collection)
{
if(!string.IsNullOrEmpty(collection["selectedShops"]))
{
string strSelectedShops = collection["selectedShops"];
}
}
Hi you can get the selected checkbox value using the bellow code it seem working fine fore me,
<script>
$(document).ready(function()
{
$("input[type=checkbox]").click(function()
{
var categoryVals = [];
categoryVals.push('');
$('#Category_category :checked').each(function() {
categoryVals.push($(this).val());
});
$.ajax({
type:"POST",
url:"<?php echo $this->createUrl('ads/searchresult'); ?>", //url of the action page
data:{'category': categoryVals},
success : function(response){
//code to do somethng if its success
}
});
}
}
</script>