I'm currently building an Html form that takes user input, creates a message body out of it, and sends it to me in an email. Since there are 10 different input fields the user needs to fill out, I'd like to concatenate the strings into a single string - messageBody. When I define the strings with { get; set; } in the model, they need to be Public so that I can access them on the form in the view. However, when I try to concatenate all of the strings in the model, I can't because "A field initializer cannot reference the non-static field." I tried changing the strings to "public static string" and it let me reference the strings in the model, but it could no longer reference the strings in the view because "cannot be accessed with an instance reference; qualify it with a type name instead." I switched them back to "public string" and thought maybe I could just concatenate them in Razor in the view itself, but when I try that, it doesn't recognize the strings because "The name "myString" doesn't exist in the current context."
I'm kind of lost on what to try next. From my understanding (which isn't that great), my strings MUST be public in order to access them from the view, but they also MUST be static for me to concatenate them into a single string? Doesn't make that much sense to me, I really hope I'm just screwing something up somewhere.
My model looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CuttingEdgeWebsite2._0.Models
{
public class ServiceRequest
{
[Key]
public int serviceRequestID { get; set; }
[Required]
public string serviceRequestName { get; set; }
[Required]
public string serviceRequestEmail { get; set; }
public string serviceRequestCompany { get; set; }
public string serviceRequestAddress { get; set; }
[Required]
public string serviceType { get; set; }
public string serviceRequestCity { get; set; }
public string serviceRequestState { get; set; }
public string serviceRequestZip { get; set; }
[Required]
public string serviceRequestNumber { get; set; }
public string serviceRequestComments { get; set; }
}
}
My View looks like:
@model MyWebsite.Models.ServiceRequest
@using (Html.BeginForm("Email", "ServiceRequestsController", FormMethod.Post))
{
<div class="servicesReq">
<div class="serviceReqForm">
<div class="col-left">
<label>
<span class="required">* </span><span>Name:</span><br />
@Html.TextBoxFor(model => model.serviceRequestName)
</label><br />
<label>
<span class="required">* </span><span>Email:</span><br />
@Html.TextBoxFor(model => model.serviceRequestEmail)
</label><br />
<label>
<span>Company:</span><br />
@Html.TextBoxFor(model => model.serviceRequestCompany)
</label><br />
<label>
<span>Address:</span><br />
@Html.TextBoxFor(model => model.serviceRequestAddress)
</label><br /><br />
</div>
<div class="serviceTypeGrid">
<span class="required">* </span><span>Please choose what type of servicing you require:</span><br /><br />
</div>
<div class="col-left2">
<label class="serviceType">
<input type="radio" name="serviceType" value="Electrical" /> Electrical<br />
<input type="radio" name="serviceType" value="Lighting" /> Lighting<br />
<input type="radio" name="serviceType" value="Energy" /> Energy<br />
<input type="radio" name="serviceType" value="Multiple" /> Multiple<br />
</label><br /><br />
</div>
<div class="col-right">
<label>
<span>City:</span><br />
@Html.TextBoxFor(model => model.serviceRequestCity)
</label><br />
<label>
<span>State:</span><br />
@Html.TextBoxFor(model => model.serviceRequestState)
</label><br />
<label>
<span>Zip Code:</span><br />
@Html.TextBoxFor(model => model.serviceRequestZip)
</label><br />
<label>
<span class="required">* </span><span>Phone Number:</span><br />
@Html.TextBoxFor(model => model.serviceRequestNumber)
</label><br />
</div>
<label class="commentText">
<span>Comments:</span><br />
@Html.TextAreaFor(model => model.serviceRequestComments, new { @class = "servReqComment" })
</label>
<div class="formSubmitButton">
<input type="submit" value="Submit" /><br />
<br />
</div>
</div>
</div>
}
Any ideas on where/how to concatenate strings in this situation would be greatly appreciated.